sqlserver 中 case when 的用法

@@sqlserver CASE WHEN

 

1.case when 的用法

(1)简单Case函数

CASE sex

WHEN '1' THEN '男'

WHEN '2' THEN '女'

ELSE '其他'

END

(2)--Case搜索函数

CASE WHEN sex = '1' THEN '男'

WHEN sex = '2' THEN '女'

ELSE '其他'

END

2.case when 代码

复制代码
1.case when 用在取字段名上
select id,username
(case when f.answerer is not null
 then 1 
 else 0
 end) as isAnswer f
from user
 
2.根据区间进行选择值
select case when type in (1,2) then 1
    when type in (3,4) then 2 
    when type in (5,6,7,8) then 3
    when type in (9,10,11,12,13,14,15) then 4 
    when type in (16,17,18) then 5
    when type in (19,20,21,22,23,24,25) then 6
    when type in (26,27,28,29) then 7
    when type in (30,31) then 8 
    when type in (32) then 9 else 9 end type,userID
from plat_teacher_BonusPoints_record with(nolock)
where  cwareID=#cwareID# and status=1
复制代码

3.isEqual 的用法(用在where查询条件下)

<isEqual prepend="and" property="selectTime" compareValue="1">
        datediff(week,createTime,getdate())=0
</isEqual>
<isEqual prepend="and" property="selectTime" compareValue="2">
        datediff(month,createTime,GETDATE())=0
</isEqual>


————————————————
版权声明:本文为CSDN博主「Michaeles」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Michaeles/article/details/85279851

 

posted @   dreamw  阅读(4250)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
历史上的今天:
2021-05-26 通过Dapr实现一个简单的基于.net的微服务电商系统
点击右上角即可分享
微信分享提示