sql case碰上的问题,很高兴找到了答案
sql case when语句的奇怪问题
有这么条语句
select top 1000 *,
'库存描述' =
CASE
WHEN stk_On_Hand <= 0 THEN '缺货'
WHEN stk_On_Hand>= 1 and stk_On_Hand < 3 THEN '少量'
WHEN stk_On_Hand>= 4 THEN '大量'
END
from wareh_stk
却会出现空值,
在这个blog找到了答案,引用下
http://www.blogjava.net/hsith/archive/2006/04/23/42566.html
4. 在Case when语句中只能出现 =、>=、<= 以及is null运算符,不能出现 <、>、<>、!=、以及is not null运算符。否则在Oracle的decode函数无法表达。
当必须使用 <, >, != ,is not null时,建议采用如下变通方法:
1)使用 !=时:例如 case when A!=B then e,
可改为: case A=b then e1 else e (间接实现 A!=B)
2)使用 < 时:例如 case when A<B then e,
可改为: case A<=b then case A=B then e1 else e (间接实现 A<B)
或 case A>=b then e1 else e (间接实现 A<B)
3)使用 > 时:例如 case when A>B then e,
可改为: case A>=b then case A=B then e1 else e (间接实现 A>B)
或 case A<=b then e1 else e (间接实现 A>B)
4)使用is not null 时:例如 case when A is not null then e,
可改为: case A is null then e1 else e (间接实现 A is not null)
特别说明:当执行大数据量的操作时,sql Server 对 case when 的执行效率极低,甚至可能会死机,因此希望大家尽量不要使用case when。
select top 1000 *,
'库存描述' =
CASE
WHEN stk_On_Hand <= 0 THEN '缺货'
WHEN stk_On_Hand>= 1 and stk_On_Hand < 3 THEN '少量'
WHEN stk_On_Hand>= 4 THEN '大量'
END
from wareh_stk
却会出现空值,
在这个blog找到了答案,引用下
http://www.blogjava.net/hsith/archive/2006/04/23/42566.html
4. 在Case when语句中只能出现 =、>=、<= 以及is null运算符,不能出现 <、>、<>、!=、以及is not null运算符。否则在Oracle的decode函数无法表达。
当必须使用 <, >, != ,is not null时,建议采用如下变通方法:
1)使用 !=时:例如 case when A!=B then e,
可改为: case A=b then e1 else e (间接实现 A!=B)
2)使用 < 时:例如 case when A<B then e,
可改为: case A<=b then case A=B then e1 else e (间接实现 A<B)
或 case A>=b then e1 else e (间接实现 A<B)
3)使用 > 时:例如 case when A>B then e,
可改为: case A>=b then case A=B then e1 else e (间接实现 A>B)
或 case A<=b then e1 else e (间接实现 A>B)
4)使用is not null 时:例如 case when A is not null then e,
可改为: case A is null then e1 else e (间接实现 A is not null)
特别说明:当执行大数据量的操作时,sql Server 对 case when 的执行效率极低,甚至可能会死机,因此希望大家尽量不要使用case when。