Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help me to understand the case statements in this query

Status
Not open for further replies.

nc297

Programmer
Apr 7, 2010
162
0
0
US
Can someone tell me what these two case statements are doing please.

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 ) )
 
WHEN typecode IN ( 1, 18 ) THEN reportsto

if typecode is 1 or 18 then give me column reportsto
otherwise

WHEN typecode IN ( 13 ) THEN officecode

if typecode is 13 then give me column officecode

ELSE NULL

otherwise give me nothing.

simi
 
Thanks so much Simian I understand it now. One can you explain this one to me:

CASE WHEN area < 10 THEN '0' + CAST(area AS CHAR(1)) ELSE CAST(area AS CHAR(2)) END AS area,
 
When area is less than 10 (i.e. 0-9), then output with a preceding 0. For example, if area = 4, then output '04'.

Otherwise, output area as two characters (e.g. if area=25, it will output '25').

This assumes that area will be 0 through 99.
If it is greater than two digits, results will be undefined.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top