How can I perform if … then in the SQL SELECT statement?
select if (obsolete = 'n' or Instock = 'Y'? 1: 0) as saleable, * from Product
Answer 1, Authority 100%
Case statement is the nearest
if analogue in SQL and is supported in all versions of SQL Server.
Select Cast ( Case WHEN OBSOLETE = 'N' OR Instock = 'Y' THEN 1. ELSE 0. END AS BIT) as saleable, * From Product.
The only case when you need to use
Cast – if you need a result in the form of a boolean value; If you are satisfied with the type
int , the solution is as follows:
select case WHEN OBSOLETE = 'N' OR Instock = 'Y' THEN 1. ELSE 0. END AS SALEABLE, * From Product.
Case You can implement in another Operator
Case and even include in the aggregated object.
select iIF (obsolete = 'n' or instock = 'y', 1, 0) as selable, * from Product