How do I perform an IF…THEN in an SQL SELECT?

前端 未结 30 1792
梦如初夏
梦如初夏 2020-11-21 22:50

How do I perform an IF...THEN in an SQL SELECT statement?

For example:

SELECT IF(Obsolete = \'N\' OR InStock = \'Y\' ? 1 :          


        
相关标签:
30条回答
  • 2020-11-21 23:13

    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 Saleable, *
    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 Saleable, *
    FROM Product
    

    CASE statements can be embedded in other CASE statements and even included in aggregates.

    SQL Server Denali (SQL Server 2012) adds the IIF statement which is also available in access (pointed out by Martin Smith):

    SELECT IIF(Obsolete = 'N' or InStock = 'Y', 1, 0) as Saleable, * FROM Product
    
    0 讨论(0)
  • 2020-11-21 23:13
    SELECT 
      CAST(
        CASE WHEN Obsolete = 'N' 
        or InStock = 'Y' THEN ELSE 0 END AS bit
      ) as Saleable, * 
    FROM 
      Product
    
    0 讨论(0)
  • 2020-11-21 23:14

    For the sake of completeness, I would add that SQL uses three-valued logic. The expression:

    obsolete = 'N' OR instock = 'Y'
    

    Could produce three distinct results:

    | obsolete | instock | saleable |
    |----------|---------|----------|
    | Y        | Y       | true     |
    | Y        | N       | false    |
    | Y        | null    | null     |
    | N        | Y       | true     |
    | N        | N       | true     |
    | N        | null    | true     |
    | null     | Y       | true     |
    | null     | N       | null     |
    | null     | null    | null     |
    

    So for example if a product is obsolete but you dont know if product is instock then you dont know if product is saleable. You can write this three-valued logic as follows:

    SELECT CASE
               WHEN obsolete = 'N' OR instock = 'Y' THEN 'true'
               WHEN NOT (obsolete = 'N' OR instock = 'Y') THEN 'false'
               ELSE NULL
           END AS saleable
    

    Once you figure out how it works, you can convert three results to two results by deciding the behavior of null. E.g. this would treat null as not saleable:

    SELECT CASE
               WHEN obsolete = 'N' OR instock = 'Y' THEN 'true'
               ELSE 'false' -- either false or null
           END AS saleable
    
    0 讨论(0)
  • 2020-11-21 23:15

    Simple if-else statement in SQL Server:

    DECLARE @val INT;
    SET @val = 15;
    
    IF @val < 25
    PRINT 'Hi Ravi Anand';
    ELSE
    PRINT 'By Ravi Anand.';
    
    GO
    

    Nested If...else statement in SQL Server -

    DECLARE @val INT;
    SET @val = 15;
    
    IF @val < 25
    PRINT 'Hi Ravi Anand.';
    ELSE
    BEGIN
    IF @val < 50
      PRINT 'what''s up?';
    ELSE
      PRINT 'Bye Ravi Anand.';
    END;
    
    GO
    
    0 讨论(0)
  • 2020-11-21 23:16

    A new feature, IIF (that we can simply use), was added in SQL Server 2012:

    SELECT IIF ( (Obsolete = 'N' OR InStock = 'Y'), 1, 0) AS Saleable, * FROM Product
    
    0 讨论(0)
  • 2020-11-21 23:16
    SELECT CASE WHEN Obsolete = 'N' or InStock = 'Y' THEN 1 ELSE 0 
                 END AS Saleable, * 
    FROM Product
    
    0 讨论(0)
提交回复
热议问题