SQL Server 2012-where子句中的Case语句 [英] SQL Server 2012 - Case statement in where clause
本文介绍了SQL Server 2012-where子句中的Case语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
SQL不是我的强项,但是我无法弄清楚为什么它不起作用.我只是想基于一个值运行一个不同的AND语句.具体来说,如果foo = 0,我想在dateDiff函数中更改datePart
SQL is not my strong suit, but I cannot figure out why this isn't working. I simply want to run a different AND statement based on a value. Specifically, I want to change the datePart in the dateDiff function if foo is = 0
SELECT foo,
bar,
test
FROM table
WHERE bar = 1,
CASE WHEN foo = 0
AND dateDiff(dd, getDate(), 2 ) < test
ELSE
AND dateDiff(hh, getDate(), 2 ) < test
END
推荐答案
尝试一下-
SELECT foo,
bar,
test
FROM [table]
WHERE bar = 1
AND (
(
foo = 0
AND
DATEDIFF(dd, GETDATE(), 2 ) < test
)
OR
DATEDIFF(hh, GETDATE(), 2 ) < test
)
这篇关于SQL Server 2012-where子句中的Case语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文