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)
 help needed !!

Author  Topic 

js.reddy
Yak Posting Veteran

80 Posts

Posted - 2011-08-12 : 02:06:06
Hi

I want to write a function which should return Years Months days.

i.e '10 : 6 : 20'

My input will be StartDate and EndDate. So i want to display the
difference in the above format.


Thanks
js.reddy






khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-08-12 : 02:09:50
use datediff(day, startdate, enddate) to find the different in terms of days and the convert to month, year accordingly


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-08-12 : 02:13:04
this will give you approx difference in yr month days
SELECT DATEDIFF(dd,@Start,@End)/365 as Yr,(DATEDIFF(dd,@Start,@End)%365)/30 AS Mnth, ((DATEDIFF(dd,@Start,@End)%365)%30) AS Days


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

js.reddy
Yak Posting Veteran

80 Posts

Posted - 2011-08-12 : 02:15:28
How you can convert days into years and months ?
all months & years are not same days right ?
leap year is 366 days. feb is 28 days.

so how it is possible!!



quote:
Originally posted by khtan

use datediff(day, startdate, enddate) to find the different in terms of days and the convert to month, year accordingly


KH
[spoiler]Time is always against us[/spoiler]



Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-08-12 : 02:17:37
quote:
Originally posted by js.reddy

How you can convert days into years and months ?
all months & years are not same days right ?
leap year is 366 days. feb is 28 days.

so how it is possible!!



quote:
Originally posted by khtan

use datediff(day, startdate, enddate) to find the different in terms of days and the convert to month, year accordingly


KH
[spoiler]Time is always against us[/spoiler]







exactly. You have to tell us how do you want to calculate. If not use visakh16 method


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

js.reddy
Yak Posting Veteran

80 Posts

Posted - 2011-08-12 : 02:29:13
Visakh, Still it will not give exact result.


quote:
Originally posted by visakh16

this will give you approx difference in yr month days
SELECT DATEDIFF(dd,@Start,@End)/365 as Yr,(DATEDIFF(dd,@Start,@End)%365)/30 AS Mnth, ((DATEDIFF(dd,@Start,@End)%365)%30) AS Days


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/



Go to Top of Page

js.reddy
Yak Posting Veteran

80 Posts

Posted - 2011-08-12 : 02:36:19
guys, here what i mean is, i want know the exact age of a person based on his date of birth.

the output should be as below:

12 years 10 Months 15 days or 12:10:15

this is what my requirement. please help me any one in this regard.

Thanks



quote:
Originally posted by js.reddy

Hi

I want to write a function which should return Years Months days.

i.e '10 : 6 : 20'

My input will be StartDate and EndDate. So i want to display the
difference in the above format.


Thanks
js.reddy








Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-08-12 : 02:39:19
look for "finding age" here
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=64762


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -