sql server的Case When Then關鍵字的使用


Case When Then

用法1:

 case [字段]
         when '字段等於相應的value' then 表達式
         when '字段等於相應的value' then 表達式
         else ''
      end 
   
eg:select sort,qty,
    case qty
      when 1 then '少'
      when 2 then '中'
      when 3 then '多'
      else '未知'
    end as [數量范圍]
  from test

用法2:

case 
         when [條件] then 表達式
         when [條件] then 表達式
         else ''
      end 
select sort,qty,
    case 
      when qty=1 then '少'
      when qty=2 then '中'
      when qty=3 then '多'
      else '未知'
    end as [數量范圍]
    from test 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM