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
 Combining fields to date

Author  Topic 

hanshuman10
Starting Member

2 Posts

Posted - 2012-05-02 : 19:22:36
Hi I have the date in 3 different fields dobday,dob month and dob year. I need to get the dob field and then the age field how can i do this

Thanks

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2012-05-02 : 20:55:01
SELECT DATEADD(MONTH, 12 * dobYear + dobMonth - 22801, dobDay - 1)



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

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2012-05-03 : 10:17:18
or


select dateadd(year,dobYear -1900,dateadd(month,dobMonth -1,dateadd(day,dobDay -1,0)))

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-03 : 14:44:45
for calculating age you can use logic like

CASE WHEN MONTH(dob)> MONTH(GETDATE())
OR (MONTH(dob)= MONTH(GETDATE()) AND DAY(dob)> DAY(GETDATE()))
THEN DATEDIFF(yy,dob,GETDATE())
ELSE DATEDIFF(yy,dob,GETDATE()) -1
END


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

Go to Top of Page

hanshuman10
Starting Member

2 Posts

Posted - 2012-05-03 : 19:07:15
quote:
Originally posted by SwePeso

SELECT DATEADD(MONTH, 12 * dobYear + dobMonth - 22801, dobDay - 1)
Hey Thanks That worked


N 56°04'39.26"
E 12°55'05.63"


Go to Top of Page
   

- Advertisement -