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
 DOB Calculation

Author  Topic 

satheesh
Posting Yak Master

152 Posts

Posted - 2011-08-15 : 09:50:53
Dear All,

I need to calculate date of birth using 'age' and 'transactiondate' field.How to find dateofbirth using age.

The below query calculate 'age' using 'dateofbirth'.But i need to find in reverse.Why i need to do this is, in my table any one field, either dateofbirth or age is 'Null'.So i need to update.

(datediff(yy,dateofbirth,getdate()))-(datepart (yy,convert(datetime,convert(int,dateadd(yy,datediff(yy,dateofbirth,getdate()),dateofbirth))-convert(int,dateadd(yy,-1,getdate())))))-1900) from cst

I managed to write the above but How to do this in reverse?Any help regarding this is highly appreciated.Many Thanks

Regards,
SG

Desired Result

Age Transactiondate date of Birth
26 15.08.2011 15.08.1985

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-08-15 : 10:03:00
Take a look at Peso's blog here where he discusses finding the number of years (or months) between any two dates. You can use the function as it is, or copy the logic and incorporate into your own query.
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2011-08-15 : 10:56:17
How can you calculate date of birth if you only know the age? You don't have enough information to calculate it.

If I told you my age was 25, what is my birthday?






CODO ERGO SUM
Go to Top of Page

satheesh
Posting Yak Master

152 Posts

Posted - 2011-08-15 : 11:07:44
Dear Michael ,

Thanks for your reply.I have 'transactiondate'.This says 'age' as on transaction day. For eg, you are purchasing some thing today. so 'transactiondate' be 15.08.2011 and your 'age' 25.By using this i need to find your birthday.ie (15.08.2011 - 25years = your b'day)Many Thanks

Regards,
SG
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2011-08-15 : 11:49:38
If you know someones age and the fact that they bought something today, how can you use that information to know what their birthday is? You just do not have enough information.

Are you assuming that they only buy something on their birthday?







CODO ERGO SUM
Go to Top of Page
   

- Advertisement -