How do I execute IF ... THEN in a SQL SELECT statement?
For example:
SELECT IF(Obsolete = 'N' OR InStock = 'Y' ? 1 : 0) AS Saleable, * FROM Product How to perform an IF ... THEN in an SQL SELECT? @Eric Labashosky
How do I execute IF ... THEN in a SQL SELECT statement?
For example:
SELECT IF(Obsolete = 'N' OR InStock = 'Y' ? 1 : 0) AS Saleable, * FROM Product How to perform an IF ... THEN in an SQL SELECT? @Eric Labashosky
The CASE statement is the closest analogue of IF 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 is if you need a result in the form of a Boolean value; if you are satisfied with the int type, the solution is as follows:
SELECT CASE WHEN Obsolete = 'N' or InStock = 'Y' THEN 1 ELSE 0 END as Saleable, * FROM Product A CASE statement can be embedded in another CASE statement, and even included in an aggregated object.
SQL Server Denali (SQL Server 2012) adds IIF statement, also present in access : (seen by Martin Smith )
SELECT IIF(Obsolete = 'N' or InStock = 'Y', 1, 0) as Selable, * from Product Translation of the answer How to perform an IF ... THEN in an SQL SELECT? @Darrel Miller .
Source: https://ru.stackoverflow.com/questions/518064/
All Articles