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 2005 Forums
 Transact-SQL (2005)
 number of months since date of birth

Author  Topic 

frankDK
Starting Member

14 Posts

Posted - 2008-09-19 : 05:38:49
Hi

Can anyone help on this one, in a sql2005 server, i need a total number of months since date of birth

My table contains the following:
id nr
1 1809061234
2 1809054321
3 1809044444

The first 6 numbers indicates date of birth:

180906 = 18 sep 2006 - has been living for 24 months
180905 = 18 sep 2005 - has been living for 36 months
180904 = 18 sep 2004 - has been living for 48 months


What i would like to have is a store procedure i could call with id as parameter.

Hope you can help?

Frank

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-09-19 : 05:46:04
What is the data type of nr ? convert it to datetime and then use datediff(month, dob, getdate()) find it


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

Go to Top of Page

frankDK
Starting Member

14 Posts

Posted - 2008-09-19 : 05:49:27
It's nvarchar(10)
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-09-19 : 05:51:55
[code]
select datediff(month,
convert(datetime, '20' + substring(nr, 5, 2) + substring(nr, 3, 2) + left(nr, 2)),
getdate())
from yourtable
[/code]



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

Go to Top of Page

frankDK
Starting Member

14 Posts

Posted - 2008-09-19 : 05:56:23
Thank you
it's working perfectly

frank
Go to Top of Page
   

- Advertisement -