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
 General SQL Server Forums
 New to SQL Server Programming
 find exact days,months,years between 2 dates

Author  Topic 

gsrinivas.
Yak Posting Veteran

56 Posts

Posted - 2008-10-25 : 03:48:50
Hi,
how can i find exact count of years,months,days from 2 different dates.


for example,
if I give one input as '1-oct-2006'
and another input as '10-oct-2007'

now my required output is : 1 year, 0 months, 10 days.

please send reply.

afrika
Master Smack Fu Yak Hacker

2706 Posts

Posted - 2008-10-25 : 04:05:00
look into datediff function and do the calculation and formatting yourself
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-25 : 04:55:08
Try this. this will give you a start to work on untill MVJ comes with a more efficient solution.

DECLARE @start datetime,@end datetime
SELECT @start='1-oct-2006',@end='10-oct-2007'
SELECT CASE WHEN MONTH(@end)<MONTH(@start)
OR DAY(@end)<DAY(@start)
THEN DATEDIFF(yy,@start,@end)-1
ELSE DATEDIFF(yy,@start,@end)
END AS years,
CASE WHEN DAY(@end)<DAY(@start)
THEN DATEDIFF(mm,DATEADD(yy,DATEDIFF(yy,0,@end),0),@end)-1
ELSE DATEDIFF(mm,DATEADD(yy,DATEDIFF(yy,0,@end),0),@end)
END AS months,
DATEDIFF(dd,DATEADD(mm,DATEDIFF(mm,0,@end),0),@end) AS Days


Go to Top of Page

gsrinivas.
Yak Posting Veteran

56 Posts

Posted - 2008-10-25 : 07:57:46
thanks for quick reply.
however my doubt is a result as single query....
ok, thanks for code..

i have another formula in single query....
but it hasn't exact output...
my formula is ..
*)My actual problem is ... find the experience of employee in
years,months,and days.(hiredate,getdate())

i found some formula which is not exact...
suppose the total days from different dates are 3862.

365)3862(10 -----(A)
----------3650
---------------
--------30)212(7-------(B)
-----------210
---------------
---------- 2 ------(C)

hahaha... this is the output 10 years 7 months 2 days.
but this may or may not correct..because the year may be a leap
year.. and i have taken month have exact 30 days which is wrong every
time......
by using this formula we can write a single query...but may not exact result..

however is your code outputs the exact result.....?
i have to check at home....hope it is working...
thank you very much for your quick reply.

-GSrinivas

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-25 : 09:16:05
quote:
Originally posted by gsrinivas.

thanks for quick reply.
however my doubt is a result as single query....
ok, thanks for code..

i have another formula in single query....
but it hasn't exact output...
my formula is ..
*)My actual problem is ... find the experience of employee in
years,months,and days.(hiredate,getdate())

i found some formula which is not exact...
suppose the total days from different dates are 3862.

365)3862(10 -----(A)
----------3650
---------------
--------30)212(7-------(B)
-----------210
---------------
---------- 2 ------(C)

hahaha... this is the output 10 years 7 months 2 days.
but this may or may not correct..because the year may be a leap
year.. and i have taken month have exact 30 days which is wrong every
time......
by using this formula we can write a single query...but may not exact result..

however is your code outputs the exact result.....?
i have to check at home....hope it is working...
thank you very much for your quick reply.

-GSrinivas




it is indeed a single select query. the first declare statement & select are for setting the date values. The final select alone is solution you want.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-10-25 : 09:27:59
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=78730


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

- Advertisement -