[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

find age between two dates and insert into new column

Posted on 2014-03-18
3
Medium Priority
?
332 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how the fundamental information of how to create a table.

650 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