Solved

find age between two dates and insert into new column

Posted on 2014-03-18
3
329 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
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 backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

705 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