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
 General SQL Server Forums
 New to SQL Server Programming
 How do you update just the YEAR in a date field?

Author  Topic 

BadBoy House
Starting Member

41 Posts

Posted - 2010-05-25 : 13:20:36
Hi all.

We have a field in our SQL database called Year End. It's a standard date field which contains the year end dates for our clients.

I want to alter just the year part of the date - leaving the day and month part as they are.

There are about 1000 clients on the database and each has a different date/year. I need to set the year for each client to be 2010. The dd/mm part must remain as it is.

Can anyone tell me how to do this via a query? I'll need to repeat this each year for the correct year.

Thanks in advance!

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-05-25 : 13:28:15
Here's an example:

DECLARE @d datetime

SET @d = '01/02/2008'

SELECT DATEADD(yy, 2010 - YEAR(@d), @d)

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

BadBoy House
Starting Member

41 Posts

Posted - 2010-05-26 : 03:32:27
And will that set the year to 2010 on EVERY date in the year_end field?
Go to Top of Page

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2010-05-26 : 03:45:03
why don't you experiment with the advice given? Tara has a very good track record here when it comes to giving good advice.

begin transaction
your new code here
select * from yourtable

view the output!
and then put in a "rollback transaction" or "commit transaction" depending on whether or not the code worked!!!
Go to Top of Page
   

- Advertisement -