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

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

    This isn't an answer, just an example of a CASE statement in use where I work. It has a nested CASE statement. Now you know why my eyes are crossed.

     CASE orweb2.dbo.Inventory.RegulatingAgencyName
        WHEN 'Region 1'
            THEN orweb2.dbo.CountyStateAgContactInfo.ContactState
        WHEN 'Region 2'
            THEN orweb2.dbo.CountyStateAgContactInfo.ContactState
        WHEN 'Region 3'
            THEN orweb2.dbo.CountyStateAgContactInfo.ContactState
        WHEN 'DEPT OF AGRICULTURE'
            THEN orweb2.dbo.CountyStateAgContactInfo.ContactAg
        ELSE (
                CASE orweb2.dbo.CountyStateAgContactInfo.IsContract
                    WHEN 1
                        THEN orweb2.dbo.CountyStateAgContactInfo.ContactCounty
                    ELSE orweb2.dbo.CountyStateAgContactInfo.ContactState
                    END
                )
        END AS [County Contact Name]
    

提交回复
热议问题