我想使用CASEinWHERE子句,但它返回错误。
CASE
WHERE
我可以在where子句中使用CASE吗?或如何解决?谢谢
SELECT * FROM hris_leave.dbo.tbl_act inner join hris_leave.dbo.tstaff on hris_leave.dbo.tstaff.s_id = hris_leave.dbo.tbl_act.s_id where case when acting_to is not null then datediff(day, acting_from, acting_to) >= 90 and acting_to >= '2010-10-01' else acting_to is null order by hris_leave.dbo.tbl_act.s_id
就您而言,您只需要OR
WHERE ( acting_to is null OR ( datediff(day, acting_from, acting_to) >= 90 AND acting_to >= '2010-10-01' ) )
一种情况是针对 值 ,而不是条件。条件在CASE表达式之外
例如
CASE WHEN SomeCol = 'a' THEN ColA WHEN SomeCol = 'c' THEN ColC ELSE ColB END > 42