您的当前位置:首页正文

SQLServer中Case的用法

来源:九壹网
一些简单的用法,有空会写完整点的
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

因篇幅问题不能全部显示,请点此查看更多更全内容

Top