欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【sql】条件判断(if,case when)

程序员文章站 2022-06-01 16:48:17
...
UPDATE [customer].[Customer]
        SET InfoCompleteUserNo = #{infoCompleteUserNo},
        InformationCompletionDate = GETDATE(),
        InfoMaintainFlag = 0,
        [DemandStateStatus]=IIF([DemandStateStatus]=0,5,[DemandStateStatus]),
        [DemandStatusChangeDate]=IIF([DemandStateStatus]=0,GETDATE(),[DemandStatusChangeDate]),
        [TakeLookLastMonthNum] = IIF([DemandStateStatus]=0,0,[TakeLookLastMonthNum]),
		[VisitLastMonthNum] = IIF([DemandStateStatus]=0,0,[VisitLastMonthNum])
        WHERE CustomerId = #{customerId} AND DeleteFlag = 0

case when 多条件编写方法
case when多条件编写语法:

case
when 条件1 and 条件2 then ‘1’
when 条件1 and 条件2 then ‘1’
else
end

**case when 多条件编写举例 **

create table [maomao365.com]
(keyId int identity,
xingBie varchar(100)
)
go

insert into [maomao365.com]
(xingbie)values('1'),
('0'),('1')
,('1'),('2')
go

select 
keyId,
case 
when xingBie ='1' or xingBie ='2' 
then N'性别'
when xingBie ='0' 
then N'未填写!'
else ''
end as xingBie 
from [maomao365.com]


go
truncate table [maomao365.com]
drop table [maomao365.com]