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 2000 Forums
 Transact-SQL (2000)
 dates difference

Author  Topic 

cognos79
Posting Yak Master

241 Posts

Posted - 2007-09-27 : 14:42:06
How do i find out if two dates are exactly 3 yrs apart or not.
stdate: 12/1/2005
enddate: 11/10/2008.
the result shld 2 yrs .

I dont care abt the days. just the months and years.

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2007-09-27 : 14:50:48
Use Datediff.

Dinakar Nethi
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-09-27 : 15:08:01
"exactly 3 yrs apart or not"

<sigh!>

What does that mean?

Same day same month 3 years ago?

What is the correct value for 29-Feb?

Kristen
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-09-27 : 15:10:52
I suppose you could do

SELECT DATEDIFF(Month, '20051201', '20081110') = 36

That satisfies the "months and years" requirement, but I don't see how that is "exactly 3 yrs apart"

Note that using DATEDIFF on a column will prevent the query using any index on that column, so performance may be sub-optimal.

Kristen
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-09-28 : 03:07:56
http://www.sqlteam.com/article/datediff-function-demystified



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -