How do you perform an IF…THEN in an SQL SELECT ?
How do we perform an IF...THEN in an SQL SELECT statement?
For example :
SELECT IF(Obsolete = 'N' or InStock = 'Y';1;0) as Salable, * FROM Product
The CASE statement is the closest to IF in SQL and is supported on all versions of SQL Server
SELECT CAST( CASE WHEN Obsolete = 'N' or InStock = 'Y' THEN 1 ELSE 0 END AS bit) as Salable, * FROM Product
You only need to do the CAST if you want the result as a boolean value, if you are happy with an int, this works:
SELECT CASE WHEN Obsolete = 'N' or InStock = 'Y' THEN 1 ELSE 0 END as Salable, * FROM Product
CASE statements can be embedded in other CASE statements and even included in aggregates.
SQL Server Denali adds the IIF statement which is also available in access:
SELECT IIF(Obsolete = 'N' or InStock = 'Y', 1, 0) as Selable, * from Product
Hope you have enjoyed to read this post,
let me know if you have more suitable suggestions.
you might be more interested in SQL, SQL Server, database
Good Day [/ Night] ! Happy Google + ing !
:D
How do we perform an IF...THEN in an SQL SELECT statement?
For example :
SELECT IF(Obsolete = 'N' or InStock = 'Y';1;0) as Salable, * FROM Product
The CASE statement is the closest to IF in SQL and is supported on all versions of SQL Server
SELECT CAST( CASE WHEN Obsolete = 'N' or InStock = 'Y' THEN 1 ELSE 0 END AS bit) as Salable, * FROM Product
You only need to do the CAST if you want the result as a boolean value, if you are happy with an int, this works:
SELECT CASE WHEN Obsolete = 'N' or InStock = 'Y' THEN 1 ELSE 0 END as Salable, * FROM Product
CASE statements can be embedded in other CASE statements and even included in aggregates.
SQL Server Denali adds the IIF statement which is also available in access:
SELECT IIF(Obsolete = 'N' or InStock = 'Y', 1, 0) as Selable, * from Product
Hope you have enjoyed to read this post,
let me know if you have more suitable suggestions.
you might be more interested in SQL, SQL Server, database
Good Day [/ Night] ! Happy Google + ing !
:D





