Modified code:
declare @D1 datetime, @D2 datetime
select @d1 = '1/30/2000 11:59:59 pm', @d2 = '1/31/2000 12:00:01 am'
select datepart(year, @d2-@d1)-1900 as years,
datepart(month, @d2-@d1)-1 as months,
datepart(day, @d2-@d1)-1 as days,
datepart(hour, @d2-@d1) as hours,
datepart(minute, @d2-@d1) as minutes,
datepart(second, @d2-@d1) as seconds,
datepart(millisecond, @d2-@d1) as milliseconds
Tried it with:
1 Jan 2000 11:59:59 PM and 2 Jan 2000 12:00:01 AM
1 Jan 1800 and 1 Jan 1900
28 Feb 2008 and 1 Mar 2008 -- To test for leap years
12 Dec 2007 and 12 Dec 2007 -- To test for 0
12 Dec 2007 and 12 Dec 2008
Stopped there. The first 4 worked perfectly. The last one gave back 1 year and 1 day. It's because of the leap year, since the Date2-Date1 float calculation gives 2 Jan 1901. Makes the whole thing not work without various Case statements.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon