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

前端 未结 30 1802
梦如初夏
梦如初夏 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: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
    

提交回复
热议问题