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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

T-SQL question on case/when statement

Status
Not open for further replies.

BrooksMT

Programmer
Jun 12, 2013
28
US
I'm much more familiar with VBA and less so with T-SQL.

Does the following code make sense? If a ‘WHEN’ statement evaluates as true, are the following WHEN statements skipped?

update CPI_Temp Set [ID_OWAM] =
CASE
WHEN [Orig_wam]< 24 THEN '01Y'
WHEN [Orig_wam]< 48 THEN '03Y'
WHEN [Orig_wam]< 72 THEN '05Y'
WHEN [Orig_wam]< 102 THEN '07Y'
WHEN [Orig_wam]< 150 THEN '10Y'
else
'15Y'
END Where PAY_TYPE = 'B';


The following code won’t compile, because it won’t let you use the “<” sign. I'd rather use CASE [Orig_wam] if possible ...

update CPI_Temp Set [ID_OWAM] =
CASE [Orig_wam]
WHEN < 24 THEN '01Y'
WHEN < 48 THEN '03Y'
WHEN < 72 THEN '05Y'
WHEN < 102 THEN '07Y'
WHEN < 150 THEN '10Y'
else
'15Y'
END Where PAY_TYPE = 'B';
 
In either case, the first WHEN wins, and the rest are skipped. The second case statement is called a Simple Case Statement. It will only allow for equality operations.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top