加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

SQLServer中Case的用法

发布时间:2020-12-12 14:56:58 所属栏目:MsSql教程 来源:网络整理
导读:一些简单的用法,有空会写完整点的 if object_id('tempdb..#test2') is not null drop table #test2create table #test2 (单号 nvarchar(20),金额 int) insert into #test2(单号,金额) values('pk1',10)insert into #test2(单号,金额) values('pk2',20)inser
一些简单的用法,有空会写完整点的
if object_id('tempdb..#test2') is not null
    drop table #test2
create table #test2
 (
单号 nvarchar(20),金额 int
)
 insert into #test2(单号,金额) values('pk1',10)
insert into #test2(单号,金额) values('pk2',20)
insert into #test2(单号,金额) values('pk3',-30)
insert into #test2(单号,金额) values('pk4',-10)
 
1、在case后面选中列,when后面为该列的值
select  单号
(
case 金额
when 10 then '收入'
when 20 then '收入'
else '支出'
end
) as 收支状况
from #test2
2、在when后面判断大小
select 单号,(case when 金额>=0 then 金额 else 0 end) as '收入',(case when 金额<0 then 金额 else 0 end) as '支出'
from #test2

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读