CASE / IF与演员

时间:2017-04-20 09:17:42

标签: sql sql-server reporting-services

我一直有错误,我不明白为什么,我希望你能帮助我。我想调整以下表达式:

CAST((pwdLastSet0 / 864000000000.0 - 109207) AS DATETIME) AS 'PasswordLastSet',dateadd(d, 80, CAST((pwdLastSet0 / 864000000000.0 - 109207) as DATETIME)) AS 'expirationdate'

因为,如果我的列值pwdlastset0为null,则无法进行计算。

所以,我尝试了两种形状:

case 
    when pwdlastset0 !=0
    then
CAST((pwdLastSet0 / 864000000000.0 - 109207) AS DATETIME) AS [Password Last Set],dateadd(d, 80, CAST((pwdLastSet0 / 864000000000.0 - 109207) as DATETIME)) AS 'futurdate'
    else
pwslastset0 = 'The password has never been set'

If pwdlastset0 !=0
then
CAST((pwdLastSet0 / 864000000000.0 - 109207) AS DATETIME) AS [Password Last Set],dateadd(d, 80, CAST((pwdLastSet0 / 864000000000.0 - 109207) as DATETIME)) AS 'futurdate'
else
pwdlastset0 = 'The password has never been set'

我尝试了没有“AS”并且也改变了我的条件,我的意思是,如果= 0然后显示'尚未设置'其他calucation。无论如何我无法让它工作,所以如果你们有任何想法,我还没有尝试过,非常感谢你!

2 个答案:

答案 0 :(得分:3)

pwdlastset0 !=0未检查NULL。使用IS NULL表达式:

case 
    when pwdlastset0 IS NOT NULL
    then
CAST((pwdLastSet0 / 864000000000.0 - 109207) AS DATETIME) AS [Password Last Set],dateadd(d, 80, CAST((pwdLastSet0 / 864000000000.0 - 109207) as DATETIME)) AS 'futurdate'
    else
pwslastset0 = 'The password has never been set'

答案 1 :(得分:0)

更改比较以检查pwdlastset0是否大于0.如果不大于0,则返回密码从未设置过。

case 
    when pwdlastset0 > 0
    then
CAST((pwdLastSet0 / 864000000000.0 - 109207) AS DATETIME) AS [Password Last Set],dateadd(d, 80, CAST((pwdLastSet0 / 864000000000.0 - 109207) as DATETIME)) AS 'futurdate'
    else
pwslastset0 = 'The password has never been set'