How do I do multiple CASE WHEN conditions using SQL Server 2008?

前端 未结 10 1899
被撕碎了的回忆
被撕碎了的回忆 2020-11-27 09:57

What I\'m trying to do is use more than one CASE WHEN condition for the same column.

Here is my code for the query:

   SELECT   Url=\'\',
                    


        
相关标签:
10条回答
  • 2020-11-27 10:25
        case when first_condition
          then first_condition_result_true
        else
          case when second_condition 
            then second_condition_result_true
          else
              second_condition_result_false                              
          end
        end
      end as qty
    
    0 讨论(0)
  • 2020-11-27 10:27

    You can use below example of case when with multiple conditions.

    SELECT
      id,stud_name,
      CASE
        WHEN marks <= 40 THEN 'Bad'
        WHEN (marks >= 40 AND
          marks <= 100) THEN 'good'
        ELSE 'best'
      END AS Grade
    FROM Result
    
    0 讨论(0)
  • 2020-11-27 10:27
    case 
        when a.REASONID in ('02','03','04','05','06') then
            case b.CALSOC 
                when '1' then 'yes' 
                when '2' then 'no' 
                else 'no' 
            end
        else 'no' 
    end 
    
    0 讨论(0)
  • 2020-11-27 10:27

    Something Like this, Two Conditions Two Columns

    SELECT ITEMSREQ.ITEM AS ITEM,
           ITEMSREQ.CANTIDAD AS CANTIDAD,
           (CASE  WHEN ITEMSREQ.ITEMAPROBADO=1 THEN 'APROBADO'
                  WHEN ITEMSREQ.ITEMAPROBADO=0 THEN 'NO APROBADO'
            END) AS ITEMS,
            (CASE 
                  WHEN ITEMSREQ.ITEMAPROBADO = 0 
                  THEN CASE WHEN REQUISICIONES.RECIBIDA IS NULL  THEN 'ITEM NO APROBADO PARA ENTREGA' END
                  WHEN ITEMSREQ.ITEMAPROBADO = 1 
                  THEN CASE WHEN REQUISICIONES.RECIBIDA IS NULL THEN 'ITEM AUN NO RECIBIDO' 
                            WHEN REQUISICIONES.RECIBIDA=1 THEN 'RECIBIDO' 
                            WHEN REQUISICIONES.RECIBIDA=0 THEN 'NO RECIBIDO' 
                           END
                  END)
                  AS RECIBIDA
     FROM ITEMSREQ
          INNER JOIN REQUISICIONES ON
          ITEMSREQ.CNSREQ = REQUISICIONES.CNSREQ
    
    0 讨论(0)
  • 2020-11-27 10:28

    Combining all conditions

    select  a.* from tbl_Company a
    
    where  a.Company_ID NOT IN (1,2)  
    
    AND (   
            (0 = 
                CASE WHEN (@Fromdate = '' or @Todate='')
                    THEN 0 
                    ELSE 1  
                END
            )      -- if 0=0 true , if 0=1 fails (filter only when the fromdate and todate is present)
                    OR
            (a.Created_Date between @Fromdate and @Todate )                 
        )
    
    0 讨论(0)
  • 2020-11-27 10:30

    This can be an efficient way of performing different tests on a single statement

    select
    case colour_txt 
      when 'red' then 5 
      when 'green' then 4 
      when 'orange' then 3
    else 0 
    end as Pass_Flag
    

    this only works on equality comparisons!

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