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
 SQL Server Development (2000)
 date diff function

Author  Topic 

thanksfor help
Posting Yak Master

106 Posts

Posted - 2006-03-03 : 12:07:05
Hi,

I want date diff between 2 dates in the following format.

no.of years, no.of months , no.of days.

For eg. 2-3-2006 and 4-7-2004 of format (mm-dd-yyyy)

The result should be

2 years, 2 months and 4 days.

Thanks

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2006-03-03 : 12:10:18
Have you looked up DATEDIFF in Books Online?
Go to Top of Page

swatib
Posting Yak Master

173 Posts

Posted - 2006-03-04 : 01:56:08
There is no direct option to get diff in ur required format.

I think you need to get diff in days and then convert the result days into mm-dd-yyyy format

Njoy Life
Go to Top of Page

lane0618
Posting Yak Master

134 Posts

Posted - 2006-03-04 : 23:43:41
There are some good examples here that might get you headed in the right direction:

http://www.informit.com/articles/article.asp?p=31453&rl=1



Create a Family Website! Share Photos, News, Polls, Calendar, Address Book and more! Visit www.familydetails.com for a free 30-day trial.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-03-05 : 00:10:01
You can refer to this thread http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=61784

----------------------------------
'KH'


Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-03-05 : 00:12:34
This is the script expanded based on MVJ's script. Do test it further.

select 	[Age] = convert(varchar, [Years])  + ' years ' +
convert(varchar, [Months]) + ' months ' +
convert(varchar, [Days]) + ' days',
*
from
(
select
[Years] = case when BirthDayThisYear <= Today
then datediff(year, BirthYearStart, CurrYearStart)
else datediff(year, BirthYearStart, CurrYearStart) - 1
end,
[Months]= case when BirthDayThisYear <= Today
then datediff(month, BirthDayThisYear, Today)
else datediff(month, BirthDayThisYear, Today) + 12
end,
[Days]= case when BirthDayThisMonth <= Today
then datediff(day, BirthDayThisMonth, Today)
else datediff(day, dateadd(month, -1, BirthDayThisMonth), Today)
end,
Birth = convert(varchar(10) ,Birth, 121),
Today = convert(varchar(10), Today, 121)
from
(
select --BirthDayThisYear = dateadd(year, datediff(year, BirthYearStart, CurrYearStart), Birth),
BirthDayThisYear =
case when day(dateadd(year, datediff(year, BirthYearStart, CurrYearStart), Birth)) <> day(Birth)
then dateadd(day, 1, dateadd(year, datediff(year, BirthYearStart, CurrYearStart), Birth))
else dateadd(year, datediff(year, BirthYearStart, CurrYearStart), Birth)
end,
-- BirthDayThisMonth = dateadd(month, datediff(month, BirthMonthStart, CurrMonthStart), Birth),
BirthDayThisMonth =
case when day(dateadd(month, datediff(month, BirthMonthStart, CurrMonthStart), Birth)) <> day(Birth)
then dateadd(day, 1, dateadd(month, datediff(month, BirthMonthStart, CurrMonthStart), Birth))
else dateadd(month, datediff(month, BirthMonthStart, CurrMonthStart), Birth)
end,
*
from
(
select BirthYearStart = dateadd(year, datediff(year, 0, Birth), 0),
CurrYearStart = dateadd(year, datediff(year, 0, Today), 0),
BirthMonthStart = dateadd(month, datediff(month, 0, Birth), 0),
CurrMonthStart = dateadd(month, datediff(month, 0, Today), 0),
*
from
(
-- Load some test date pairs
select Birth = convert(datetime, '1960-02-29'),
--Today = dateadd(day, 0, datediff(day, 0, getdate()))
Today = convert(datetime, '2006-03-01') union all
select convert(datetime, '2005-03-01'), convert(datetime, '2006-02-27')
) aaaa
) aaa
) aa
)a


----------------------------------
'KH'


Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2006-03-05 : 00:34:27
quote:
Originally posted by thanksfor help

Hi,

I want date diff between 2 dates in the following format.

no.of years, no.of months , no.of days.

For eg. 2-3-2006 and 4-7-2004 of format (mm-dd-yyyy)

The result should be

2 years, 2 months and 4 days.

Thanks



What is your algorithim for calculating the difference?

By my calculation, the difference between 4-7-2004 and 2-3-2006 should be 1 year, 9 months, and 27 days.



select End_Date = dateadd(dd,27,dateadd(mm,9,dateadd(yy,1,'2004-04-07')))

End_Date
------------------------------------------------------
2006-02-03 00:00:00.000

(1 row(s) affected)




CODO ERGO SUM
Go to Top of Page
   

- Advertisement -