Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
How to calculate the age with an if condition under select queryIf isdate(ToContractorDate) = 1 then DATEDIFF(Day, TAB_ccsNetSU.PDReceivedDate, ToContractorDate) AS Ageelseif isdate(ToContractorDate) = 0 then DATEDIFF(Day, TAB_ccsNetSU.PDReceivedDate, GETDATE()) AS Ageend ifI would like to include the above if condition to AGE in the following query can you please help: this is in my stored proc where i am using dynamic string.SET @SQLStatement = 'SELECT TAB_ccsNetSU.SUNumber, TAB_ccsNetSU.SUTitle, DATEDIFF(Day, TAB_ccsNetSU.PDReceivedDate, GETDATE()) AS Age FROM TAB_ccsNetSU INNER JOINTAB_ccsNetActions ON TAB_ccsNetSU.SUID = TAB_ccsNetActions.ModuleRecordID ' + @SQLJoin
TG
Master Smack Fu Yak Hacker
6065 Posts
Posted - 2006-01-13 : 16:29:05
Are you looking for CASE?
select case when isdate(ToContractorDate) = 1 then DATEDIFF(Day, TAB_ccsNetSU.PDReceivedDate, ToContractorDate) else DATEDIFF(Day, TAB_ccsNetSU.PDReceivedDate, GETDATE()) end as Age