Is there an equivalent of the case construct in tSql?
-
The Case construct I am referring to is like this: Case {expression} {result 1} : {code block} {result 2} : {code block} {result 3} : {code block} {result 4} : {code block} Else {code block} End Case I would like to do something like this in tSQL on SQL Server 2000. Rather than using daisy chained If statements. Any ideas? Jason Jystad Cito Technologies www.citotech.net Sonork ID 11.9918 >-------------------------------------------------< Every program has at least one bug and can be shortened by at least one instruction -- from which, by induction, one can deduce that every program can be reduced to one instruction that doesn't work. >-------------------------------------------------<
-
The Case construct I am referring to is like this: Case {expression} {result 1} : {code block} {result 2} : {code block} {result 3} : {code block} {result 4} : {code block} Else {code block} End Case I would like to do something like this in tSQL on SQL Server 2000. Rather than using daisy chained If statements. Any ideas? Jason Jystad Cito Technologies www.citotech.net Sonork ID 11.9918 >-------------------------------------------------< Every program has at least one bug and can be shortened by at least one instruction -- from which, by induction, one can deduce that every program can be reduced to one instruction that doesn't work. >-------------------------------------------------<
In SQL Server BOL, click the index tab then type CASE. Thats it. In case you don't have BOL installed, here's a site. http://www.sqlteam.com/item.asp?ItemID=922 Andy Gaskell, MCSD
-
In SQL Server BOL, click the index tab then type CASE. Thats it. In case you don't have BOL installed, here's a site. http://www.sqlteam.com/item.asp?ItemID=922 Andy Gaskell, MCSD
Thanks! You are correct, I do not have access to the BOL from where I am. Thanks for the site, I was looking for something like that online, but I wasn't having much luck. Jason Jystad Cito Technologies www.citotech.net Sonork ID 100.9918 >-------------------------------------------------< Every program has at least one bug and can be shortened by at least one instruction -- from which, by induction, one can deduce that every program can be reduced to one instruction that doesn't work. >-------------------------------------------------<
-
The Case construct I am referring to is like this: Case {expression} {result 1} : {code block} {result 2} : {code block} {result 3} : {code block} {result 4} : {code block} Else {code block} End Case I would like to do something like this in tSQL on SQL Server 2000. Rather than using daisy chained If statements. Any ideas? Jason Jystad Cito Technologies www.citotech.net Sonork ID 11.9918 >-------------------------------------------------< Every program has at least one bug and can be shortened by at least one instruction -- from which, by induction, one can deduce that every program can be reduced to one instruction that doesn't work. >-------------------------------------------------<
-
The Case construct I am referring to is like this: Case {expression} {result 1} : {code block} {result 2} : {code block} {result 3} : {code block} {result 4} : {code block} Else {code block} End Case I would like to do something like this in tSQL on SQL Server 2000. Rather than using daisy chained If statements. Any ideas? Jason Jystad Cito Technologies www.citotech.net Sonork ID 11.9918 >-------------------------------------------------< Every program has at least one bug and can be shortened by at least one instruction -- from which, by induction, one can deduce that every program can be reduced to one instruction that doesn't work. >-------------------------------------------------<
YES... Like this
Select
CASE FieldNumber
WHEN 0 THEN "Cero"
WHEN 1 THEN "One"
.
.
.
WHEN 9 THEN "nine"
ELSE "No Value"
END
From
MyTableBest Regards Carlos Antollini.
-
The Case construct I am referring to is like this: Case {expression} {result 1} : {code block} {result 2} : {code block} {result 3} : {code block} {result 4} : {code block} Else {code block} End Case I would like to do something like this in tSQL on SQL Server 2000. Rather than using daisy chained If statements. Any ideas? Jason Jystad Cito Technologies www.citotech.net Sonork ID 11.9918 >-------------------------------------------------< Every program has at least one bug and can be shortened by at least one instruction -- from which, by induction, one can deduce that every program can be reduced to one instruction that doesn't work. >-------------------------------------------------<
Yes, you are correct, it is a lot eaiser to write a Case statement than a nested if statement, especially inside those small stored procedure window of SQL Server 2000.
(CASE WHEN Cust_Name = 1 THEN 'Good Customer' ELSE 'BAD Customer' END)
or you can do nested Case statements such as:
(CASE WHEN Cust_Name = 1 then 'Good Customer' ELSE(CASE WHEN Cust_Name = 2 THEN 'SO-SO Customer' ELSE 'Bad Customer' END) END)
Hope this helps. Nick Parker