Solved

find age between two dates and insert into new column

Posted on 2014-03-18
3
327 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 40

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 40

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

733 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