serveur sql - Comment effectuer un IF... THEN in an SQL SELECT

Mots clés : sqlsql-servertsqlif-statementcasesql

meilleur 5 Réponses serveur sql - Comment effectuer un IF... THEN in an SQL SELECT

vote vote

100

SELECT CAST(              CASE                   WHEN Obsolete = 'N' or InStock = 'Y'                      THEN 1                   ELSE 0              END AS bit) as Saleable, * FROM Product 
SELECT CASE             WHEN Obsolete = 'N' or InStock = 'Y'                THEN 1                ELSE 0        END as Saleable, * FROM Product 
SELECT IIF(Obsolete = 'N' or InStock = 'Y', 1, 0) as Saleable, * FROM Product 
vote vote

90

SELECT CASE <variable> WHEN <value>      THEN <returnvalue>                        WHEN <othervalue> THEN <returnthis>                                          ELSE <returndefaultcase>        END AS <newcolumnname> FROM <table> 
SELECT CASE WHEN <test>      THEN <returnvalue>             WHEN <othertest> THEN <returnthis>                              ELSE <returndefaultcase>        END AS <newcolumnname> FROM <table> 
vote vote

75

SELECT IIF(Obsolete = 'N' OR InStock = 'Y', 1, 0) AS Salable, * FROM   Product 
IF @IncludeExtendedInformation = 1   BEGIN       SELECT A,B,C,X,Y,Z       FROM   T   END ELSE   BEGIN       SELECT A,B,C       FROM   T   END 
vote vote

66

SELECT     FirstName, LastName,     Salary, DOB,     CASE Gender         WHEN 'M' THEN 'Male'         WHEN 'F' THEN 'Female'     END FROM Employees 
vote vote

57

SELECT Salable =         CASE Obsolete         WHEN 'N' THEN 1         ELSE 0     END 

Questions similaires