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

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]
发布了52 篇原创文章 · 获赞 17 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_40110781/article/details/103461670