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
 New Field

Author  Topic 

jcb267
Constraint Violating Yak Guru

291 Posts

Posted - 2009-01-18 : 11:39:55
I am using SQL 2005 (the free version). I created a table to store customers. I have a field in my table for DOB and would like to add a field for AGE and calculate age by subtracting the customer's DOB from the current date. Can anyone help me do that?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-18 : 11:45:12
just use

UPDATE Customer
SET Age=CASE WHEN MONTH(DOB)>MONTH(GETDATE())
OR (MONTH(DOB)=MONTH(GETDATE()) AND DAY(DOB)>DAY(GETDATE()))
THEN DATEDIFF(yy,DOB,GETDATE())-1
ELSE DATEDIFF(yy,DOB,GETDATE())
END
Go to Top of Page
   

- Advertisement -