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
 calculate age from 2 columns

Author  Topic 

tsouk11
Starting Member

2 Posts

Posted - 2008-02-07 : 07:11:33
hello i`m rather new in sql and i have to do something simple for u i guess.
i have 3 columns a)date of birth b)current date c)final age.
i have the values of the first 2 columns and i want column c to be calculated automaticaly when i enter the values of the first 2 columns. example.
a)20/03/1972 b)07/02/2008 c)?? (should be 36).

if you could help me i would appriciate very much.
thanks in addvance

sunil
Constraint Violating Yak Guru

282 Posts

Posted - 2008-02-07 : 07:34:52
You need to look at following article:

http://www.sqlteam.com/article/datediff-function-demystified
Go to Top of Page

tsouk11
Starting Member

2 Posts

Posted - 2008-02-07 : 07:50:51
sorry but as i told you i`m very new on sql and i read the article but i still don`t know what to do to make column c calculated auto.
please help
Go to Top of Page

sunil
Constraint Violating Yak Guru

282 Posts

Posted - 2008-02-07 : 08:02:28
If you are using Select ,then, you can use like:
Declare @dt table
(
Date1 datetime,
Date2 datetime
)
Insert @dt
select '03/12/1972','03/10/2008'

select date1,date2,DateDiff(year,Date1,date2) [AGE] from @dt

If you are inserting into table then
Declare @dt table
(
Date1 datetime,
Date2 datetime,
age int
)


Insert into @dt(date1,date2,age) values ('03/12/1972','03/10/2008',DateDiff(year,'03/12/1972','03/10/2008'))
select * from @dt

But, make sure you read and understand it. It shows how DateDiff can give you incorrect results.
Go to Top of Page
   

- Advertisement -