sqlserver 关于 case when is null 的查询
select * from ApInterSkuInfo where BeginValue=convert(varchar(100),convert(datetime,'2017-12-09 23:59:59.8',101),23) ----更新时间为null时按照新建时间排序,否则按照更新时间排序 order by (case UpdateDate when null then CreateDate else UpdateDate end) asc
=====================================================
--这样的情况会出现null值 select (case UpdateTime when null then InsertTime end) from WxUsers where UserID='5537821' --加上else就不会出现null值 select (case UpdateTime when null then InsertTime else InsertTime end) from WxUsers where UserID='5537821'
=====================================================
SourceID='' select ISNULL(SourceID,InsertTime) from WxUsers where UserID='5537821' --查询结果也是'',因为ISNULL函数不会判断''值
=====================================================
关于 ISNULL 和 COALESCE
--SQL Server ISNULL函数和Coalesce函数替换空值的区别 SELECT COALESCE('',0) --结果0 SELECT COALESCE(' ',0) --结果0 SELECT COALESCE(null,0) --结果0 SELECT COALESCE(123,0) --结果123 SELECT ISNULL('',0) --结果'' SELECT ISNULL(' ',0) --结果'' SELECT ISNULL(null,0) --结果0 SELECT ISNULL(123,0) --结果123 --由结果结果可以看出COALESCE函数对于空值处理和NULL值都起作用。
=====================================================
关于空值的查询
--没有'',也没有' ', select * from BankPayUserFlow order by CreateDate desc --249行(只有两种情况:有内容,NULL) select * from BankPayUserFlow where SuccessTranID is null --237行 select * from BankPayUserFlow where SuccessTranID is not null --12行 select * from BankPayUserFlow where SuccessTranID ='' --0行 select * from BankPayUserFlow where SuccessTranID <>'' --12行 这行情况是特殊的,自动排除了NULL select * from BankPayUserFlow where SuccessTranID is not null or SuccessTranID<>'' --12行 --有一行是'', select * from BankPayUserFlow order by CreateDate desc --249行(只有三种情况:有内容,NULL,'') select * from BankPayUserFlow where SuccessTranID is null --236行 select * from BankPayUserFlow where SuccessTranID is not null --13行 select * from BankPayUserFlow where SuccessTranID ='' --1行 select * from BankPayUserFlow where SuccessTranID <>'' --12行 这行情况是特殊的,自动排除了NULL select * from BankPayUserFlow where SuccessTranID is not null or SuccessTranID<>'' --13行 --有一行是'',有一行是' ', select * from BankPayUserFlow order by CreateDate desc --249行(只有四种情况:有内容,NULL,'',' ') select * from BankPayUserFlow where SuccessTranID is null --235行 select * from BankPayUserFlow where SuccessTranID is not null --14行 select * from BankPayUserFlow where SuccessTranID ='' --2行 select * from BankPayUserFlow where SuccessTranID <>'' --12行 这行情况是特殊的,自动排除了NULL select * from BankPayUserFlow where SuccessTranID is not null or SuccessTranID<>'' --14行
搜索
复制
标签:case,SuccessTranID,--,sqlserver,when,BankPayUserFlow,null,where,select From: https://www.cnblogs.com/jankie1122/p/11108842.html