Solved

find age between two dates and insert into new column

Posted on 2014-03-18
3
321 Views
Last Modified: 2014-03-19
I added an existing column to my DB called AgeReg.   Short for Age of Registration.

I have two other columns which is the Date of Entry (DOE) and Date of Birth (DOB)

How do I run a command to go through the entire table and find the Age relative to the the Date of Entry and then insert it into the the AgeReg column
0
Comment
Question by:al4629740
  • 2
3 Comments
 
LVL 39

Accepted Solution

by:
lcohan earned 500 total points
ID: 39937868
For instance if you want to find the diff in days between the two dates you can run a function like:

select datediff(day, GETDATE(), (GETDATE()+25))


For different "interval" you can get hours, months, seconds, etc - whatever is supported by DATEDIFF SQL function and to run an update you will do something like:

--to see what will be updated run
select AgeReg, datediff(day, DOE,DOB) as AgeReg_AfterUpdate from tablename

--then run the update
update tablename set AgeReg = datediff(day, DOE,DOB)
0
 

Author Comment

by:al4629740
ID: 39937892
Need to flipflop and change first type

select AgeReg, datediff(year, DOB,DOE) as AgeReg_AfterUpdate from tablename
0
 
LVL 39

Expert Comment

by:lcohan
ID: 39937908
Sure, if you want that AgeReg in years just make that data type an INT and then the update should look like:

-to see what will be updated run
select AgeReg, datediff(year, DOB,DOE)  as AgeReg_AfterUpdate from tablename

--then run the update
update tablename set AgeReg = datediff(year, DOB,DOE)
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now