I have a SQL statement that determines the number of days between two dates. If the difference is negative, the statement needs to select 0. This statement works but I was wondering if it is possible to assign the value of datediff("D",GETDATE(),dbo.tblKBX_Reward.validdate) to a @ variable so I don’t have to call it twice.
SELECT CASE
WHEN datediff("D",GETDATE(),dbo.tblKBX_Reward.validdate) < 0 THEN 0
ELSE datediff("D",GETDATE(),dbo.tblKBX_Reward.validdate)
END from ...
Please don’t ask why I am calculating this value using SQL instead of in code.
Thanks
You could move the case statement inside the calculation, like this: