SQL 语句case when


简介

case when 一般有两种书写方式,多用于查询判断

1.

case  列名
when  ''  then ''
when  '1' then '成功'
when  '2' then '失败'
else '其他'
end as '列名'

2.

case  
when 列名 is null then ''
when 列名 ='1' then '成功'
when 列名 ='2' then '失败'
else '未知' 
end as '列名'

实例

  
  select a.Base_Code,a.BaseName,case when  LEN(d.Spell) >=3 then rtrim(d.Spell)+' 长度大于等于3' else  rtrim(d.Spell)+' 长度小于3' end  as '拼音码',a.style,a.odate,case b.QR_code  when '' then '错误' else b.QR_code end as '二维码' from [WlData].[dbo].[Tbl_OperateInventory] as a
   inner join Tbl_QRManage as b on a.QR_Code = b.QR_code 
   inner join Tbl_dic_BaseInfo as c on a.Base_Code = c.Base_Code
   inner join Tbl_dic_Alias as d on a.Base_Code  = d.AliasCode   where a.StockType='g'

 结论  null的判断只能  通过第二种方法去判断

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM