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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Coalesce with DateDiff

Author  Topic 

evanburen
Posting Yak Master

167 Posts

Posted - 2011-03-23 : 18:50:37
Hi

I’ve been using this code to determine the number of days since the value in DateAssigned. Now the client wants to use DateReassigned if there is a value and if not continue to use DateAssigned. I thought I should use COALESCE(DateAssigned, DateReassigned) AS DaysSinceAssigned but I don’t know how to work the DATEDIFF function in there as well.

CASE
WHEN DateClosed IS NULL Then DATEDIFF(day, DateAssigned, GETDATE())

WHEN DateClosed IS NOT NULL Then NULL
END AS DaysSinceAssigned,

Bad Attempt:

COALESCE(DATEDIFF(day, DateAssigned, GETDATE(), DATEDIFF(day, ReDateAssigned, GETDATE()) AS DaysSinceAssigned

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-03-23 : 18:54:27
Could you show us some sample data? I'm confused what your intention is with DATEDIFF. Do you want that function over either column or just one?Sample data will help clear this up.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

evanburen
Posting Yak Master

167 Posts

Posted - 2011-03-23 : 19:06:10
DateClosed is just any date e.g. '03/11/2011' and then I use DATEDIFF to calculate the number of days from today's date so DaysSinceAssigned value would be 12 for today. I want to use the DATEDIFF function over either DateReAssigned or DateAssigned (in that order) but not both.

Thanks
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-03-23 : 19:31:57
DATEDIFF(dd, GETDATE(), COALESCE(DateReAssigned, DateAssigned))

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -