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)
 Find the difference between two dates as days?

Author  Topic 

zeeshan13
Constraint Violating Yak Guru

347 Posts

Posted - 2010-12-20 : 13:37:43
Hi All,

I have a Table called Sales wioth following fields.

ZoneID (nvarchar)
ProductID (nvarchar)
Price (nvarchar)
StartDate (datetime)
RevertDate (datetime)


In my SELECT query I want to get all records and a new column called 'Date_Diff' which would subtract the StartDate from RevertDate and display the results as numbers (in days). So lets say the Startdate is 2010-01-10 and Revertdate is 2010-10-20 then the Date_Diff should be 10. How can I do that in my SELECT query?

Please suggest.

Thanks,

Zee

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-12-20 : 14:08:51
datediff(day,StartDate,RevertDate) as Date_Diff


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2010-12-20 : 14:27:10
datediff(day,StartDate,RevertDate) + 1 as Date_Diff

To get the op's desired result

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2010-12-20 : 18:44:28
quote:
Originally posted by zeeshan13

So lets say the Startdate is 2010-01-10 and Revertdate is 2010-10-20 then the Date_Diff should be 10.


How can this be 10 days difference?



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

zeeshan13
Constraint Violating Yak Guru

347 Posts

Posted - 2010-12-20 : 22:56:09
Sorry Peso. It was a typo what I meant was that in my example th Startdate is 2010-01-10 and Revertdate is 2010-01-20 then the Date_Diff should be 10.
Webfred has provided a correct TSQL query :) thanks.
Go to Top of Page
   

- Advertisement -