30

I have a list of birthdays and the year is the only part that is incorrect. I have a list of ID #s for these individuals. Is there a way to change only the year for all of these people? I was thinking something like making a table of the query results and then using an UPDATE SET query, but I don't know how to only change the year.

enter image description here

Sample included in edit. Every year needs to be decreased by 2.

wootscootinboogie
  • 8,461
  • 33
  • 112
  • 197

3 Answers3

52

If all rows need to be decreased by two years, then:

UPDATE dbo.TableToUpdate
  SET [Date Column] = DATEADD(YEAR, -2, [Date Column]);

If all rows need to be set to a specific year (say, 2019), and the column is date:

UPDATE dbo.TableToUpdate
  SET [Date Column] = DATEFROMPARTS(2019, MONTH([Date Column]), DAY([Date Column]);

If all rows need to be set to a specific year (say, 2019) and the column is not date, you could use DATETIMEFROMPARTS or SMALLDATETIMEFROMPARTS, but at that point the following becomes shorter:

UPDATE dbo.TableToUpdate
  SET [Date Column] = DATEADD
  (
    YEAR, 
    -DATEDIFF(YEAR, '20190101', [Date Column]), 
    [Date Column]
  );
Aaron Bertrand
  • 272,866
  • 37
  • 466
  • 490
  • Can you explain this a little, please? I assume the [fix] is the correct, year? – wootscootinboogie Apr 30 '12 at 18:05
  • I have no idea, you need to tell us how you know what to correct each date to. You say it is incorrect, but where is the correct source? – Aaron Bertrand Apr 30 '12 at 18:06
  • No, I haven't said it's incorrect, just for a little clarification on the jargon :) e.g. src.id (is myTable.id) – wootscootinboogie Apr 30 '12 at 18:07
  • 1
    You said in your question: `I have a list of birthdays and the year is the only part that is incorrect.` OK, show us some examples, show us how to fix them. Are they all 5 years off, 10 years off, or is each row off by a different amount, and how do you know what the correct year should be? SHOW US. Without details we're just stabbing. – Aaron Bertrand Apr 30 '12 at 18:09
  • This is what I was looking for. Danke. – wootscootinboogie Apr 30 '12 at 18:18
  • @AaronBertrand Can you please tell me how to set a specific year, i.e. I want to set year 2019 in all columns. – Half Blood Prince Feb 27 '20 at 09:08
  • @HalfBloodPrince See udpate. – Aaron Bertrand Feb 27 '20 at 14:13
  • @AaronBertrand Thanks for the update. Actually my column is of type 'TimeStamp'. So following query worked for me. ::: UPDATE table_name SET timestamp_column = ADDDATE(timestamp_column, INTERVAL (YEAR("2019-01-01") - YEAR(timestamp_column)) year) where YEAR(timestamp_column) > 2019;` – Half Blood Prince Mar 02 '20 at 06:08
  • @HalfBloodPrince Great, but this question was about Microsoft SQL Server. That syntax looks like some other platform. – Aaron Bertrand Mar 02 '20 at 12:37
27

Here's the shortest I can think of, change the year to 2050:

select 
    dateadd(year, (2050 - year(d)), d)            
from x

Sample data:

create table x(d date);

insert into x values
('2001-11-19'),
('2020-07-05'),
('2012-05-01');

Output:

COLUMN_0
2050-11-19
2050-07-05
2050-05-01

Live test: http://www.sqlfiddle.com/#!3/9a8b4/2

Michael Buen
  • 38,643
  • 9
  • 94
  • 118
  • 2
    That's the request, change only the year – Michael Buen Apr 30 '12 at 18:18
  • If database is just a bit object-oriented, we can do this: `UPDATE x SET d.Year = 2050`. Can only dream :-) – Michael Buen Apr 30 '12 at 18:19
  • But change every individual date by the same number of years, not change every date to be the same year. And it's not object-oriented, it's SQL Server. – Aaron Bertrand Apr 30 '12 at 18:19
  • Ok, I see now what you are saying, the question is edited now. The exact requirement is in modified question :-) – Michael Buen Apr 30 '12 at 18:21
  • "And it's not object-oriented, it's SQL Server" <-- I know it's not object-oriented :-) I'm just pining for it, data could have more structure if it is object-oriented ;-) – Michael Buen Apr 30 '12 at 18:24
  • Don't hold your breath, there is going to be a lot of pushback for that, even though you do see some non-relational and semi-structured features creeping into SQL Server. – Aaron Bertrand Apr 30 '12 at 18:26
  • Ironically I found this QA looking for an example to change only the date's year to one specific year. Fortunately I found this answer in this similar question: http://stackoverflow.com/questions/10550368/updating-year-in-datetime-variable – Hagelt18 Jul 22 '15 at 17:11
5

In case anyone else needs it for MySQL, here's the syntax:

UPDATE dbo.TableToUpdate 
SET    [Date Column] = DATE_ADD([Date Column], INTERVAL -2 year); 
WhatsThePoint
  • 3,395
  • 8
  • 31
  • 53
Matt Welander
  • 8,234
  • 24
  • 88
  • 138