sql server - How do I perform an IF...THEN in an SQL SELECT? -


how perform if...then in sql select statement?

for example:

select if(obsolete = 'n' or instock = 'y' ? 1 : 0) saleable, * product 

the case statement closest if in sql , supported on versions of sql server

select cast(              case                    when obsolete = 'n' or instock = 'y'                       1                    else 0               end bit) saleable, *  product 

you need cast if want result boolean value, if happy int, works:

select case              when obsolete = 'n' or instock = 'y'                 1                 else 0         end saleable, *  product 

case statements can embedded in other case statements , included in aggregates.

sql server denali (sql server 2012) adds iif statement available in access: (pointed out martin smith)

select iif(obsolete = 'n' or instock = 'y', 1, 0) saleable, * product 

Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -