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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 script to update date, incrementing by 1 year

Author  Topic 

spinningtop
Starting Member

29 Posts

Posted - 2011-12-16 : 05:31:39


Hi

I have an age (int) column from 0-100 and I have a date (date) column, currently NULL. I would like to update the date column with a date incrementing by 1 year.
So starting with 01/01/1900 the date column would increment as below up to age 100. I know DATEADD should be used here but I'm not exactly sure how to implement it.


age date
0 01/01/1900
1 01/01/1901
2 01/01/1902
etc....


Any help would be appreciated

Thanks


senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2011-12-16 : 05:39:12
Update table_name set date_column=dateadd(yy,age,'01/01/1900')

Senthil Kumar C
------------------------------------------------------
MCITP - Database Administration SQL SERVER 2008
MCTS - Database Development SQL SERVER 2008
Go to Top of Page

spinningtop
Starting Member

29 Posts

Posted - 2011-12-16 : 06:06:17

Great,

Thanks Senthil

I didn't realize it was so simple. I was expecting to have to use a cursor or while loop or something.

Go to Top of Page

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2011-12-16 : 06:08:00
quote:
Originally posted by spinningtop


Great,

Thanks Senthil

I didn't realize it was so simple. I was expecting to have to use a cursor or while loop or something.





Welcome

Senthil Kumar C
------------------------------------------------------
MCITP - Database Administration SQL SERVER 2008
MCTS - Database Development SQL SERVER 2008
Go to Top of Page
   

- Advertisement -