Combining multiple condition in single case statement in Sql Server

前端 未结 2 1659
伪装坚强ぢ
伪装坚强ぢ 2021-01-11 12:07

According to the following description I have to frame a CASE...END statement in SQL server , help me to frame a complex CASE...END statement to fu

相关标签:
2条回答
  • 2021-01-11 12:48
    select ROUND(CASE 
    
    WHEN  CONVERT( float, REPLACE( isnull( value1,''),',',''))='' AND CONVERT( float, REPLACE( isnull( value2,''),',',''))='' then  CONVERT( float, REPLACE( isnull( value3,''),',',''))
    
    WHEN  CONVERT( float, REPLACE( isnull( value1,''),',',''))='' AND CONVERT( float, REPLACE( isnull( value2,''),',',''))!='' then  CONVERT( float, REPLACE( isnull( value3,''),',',''))
    
    WHEN  CONVERT( float, REPLACE( isnull( value1,''),',',''))!='' AND CONVERT( float, REPLACE( isnull( value2,''),',',''))='' then  CONVERT( float, REPLACE( isnull( value3,''),',',''))
    
    else CONVERT( float, REPLACE(isnull( value1,''),',','')) end,0)  from Tablename where    ID="123" 
    
    0 讨论(0)
  • 2021-01-11 12:51

    You can put the condition after the WHEN clause, like so:

    SELECT
      CASE
        WHEN PAT_ENT.SCR_DT is not null and PAT_ENTRY.ELIGIBILITY is null THEN 'Favor'
        WHEN PAT_ENT.SCR_DT is not null and PAT_ENTRY.EL = 'No' THEN 'Error'
        WHEN PAT_ENTRY.EL = 'Yes' and ISNULL(DS.DES, 'OFF') = 'OFF' THEN 'Active'
        WHEN DS.DES = 'N' THEN 'Early Term'
        WHEN DS.DES = 'Y' THEN 'Complete'
      END
    FROM
      ....
    

    Of course, the argument could be made that complex rules like this belong in your business logic layer, not in a stored procedure in the database...

    0 讨论(0)
提交回复
热议问题