Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Understanding a case statement

Author  Topic 

JJ297
Aged Yak Warrior

940 Posts

Posted - 2010-11-23 : 16:12:14
I use a view all the time and want to begin to understand what I'm doing. I'm confused regarding the case statement. Is this correct...

When 1 or 18 are in the Typecode field then grab what's in the Reportsto field and call those results Dist

when 13 is in the Typecode field then grab what's in the officecode field and call those results Dist.

Is this what this view is doing below? I don't understand the second statement either. Please explain.


SELECT c.officecode AS doc,

CASE
WHEN typecode IN ( 1, 18 ) THEN reportsto
WHEN typecode IN ( 13 ) THEN officecode
ELSE NULL
END AS dist,

c.region AS reg, r.regionacronym,

CASE
WHEN area < 10 THEN '0' + CAST(area AS CHAR(1))
ELSE CAST(area AS CHAR(2))
END AS area,

REPLACE(c.name, ',', '') AS doc_name,
r.regionnumber AS region_num, r.regionname,
c.typecode
FROM offices.dbo.officecodes AS c
INNER JOIN offices.dbo.regions AS r
ON c.region = r.regionletter
WHERE ( c.typecode IN ( 1, 18, 13 ) )




Here's some data from the officecodes table


CREATE TABLE [dbo].[OfficeCodes](
[OfficeCode] [char](3) NOT NULL,
[Name] [varchar](50) NULL,
[TypeCode] [int] NULL,
[ReportsTo] [char](3) NULL,
[Region] [char](1) NULL,
[Area] [int] NULL)



insert into OfficeCodes
select '001', 'Portland ME', '13','H03', 'A', '3' union all
select '002', 'BanGor ME', '13', 'H03','A', '3' union all
select '003', 'Augusta ME', '13', 'H03', 'A', '3' union all
select '004', 'Auburn ME', '1', '001', 'A', '3' union all
select '005', 'Rockland ME', '1', '002', 'A', '3' union all
select '006', 'Meadville PA', '1', '205', 'c', '4' union all
select '638', 'Houghton MI', '18', '371', 'E', '1' union all
select '672', 'Key West FL', '18', '261', 'D', '2' union all
select 'A02', 'Globe AZ', '18', '912', 'J', '8'






Thanks!

chadmat
The Chadinator

1974 Posts

Posted - 2010-11-23 : 17:15:35
You are correct about the first CASE. The second is just making sure area is a 2 digit number, if it is 0-9, then it appends a leading 0 so it will show up as 00-09.

-Chad
Go to Top of Page

JJ297
Aged Yak Warrior

940 Posts

Posted - 2010-11-23 : 18:54:33
Okay thanks chatmat!
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-11-26 : 03:18:54
Also note that CASE is an expression and not a statement

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

JJ297
Aged Yak Warrior

940 Posts

Posted - 2010-11-29 : 15:56:39
Two more questions...what do you mean that case is an expression.

Second question

What does CAST mean and what is it doing in this expression?

CASE
WHEN area < 10 THEN '0' + CAST(area AS CHAR(1))
ELSE CAST(area AS CHAR(2))
END AS area,
Go to Top of Page

chadmat
The Chadinator

1974 Posts

Posted - 2010-11-29 : 16:07:35
CAST converts the datatype from some DataType, to A different Datatype. In this case, it is probably from Int to Char(1) or Char(2).

-Chad
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2010-11-29 : 19:01:40
quote:
Originally posted by JJ297

Two more questions...what do you mean that case is an expression.

According to the dictionary:
A [Computer] exrpession is a combination of variables, constants, and functions linked by operation symbols and any required punctuation that describe a rule for calculating a value.

Microsoft also calls the Case Expresson a Function.

This is in contrast to a Statement which is an instruction or other elementary component in a high-level programming language. Like a SELECT statement for example.
Go to Top of Page

JJ297
Aged Yak Warrior

940 Posts

Posted - 2010-11-29 : 19:21:04
Thanks Chad and Lamprey!
Go to Top of Page
   

- Advertisement -