Solved

How to replace the year in a date in SQL Server

Posted on 2014-12-16
8
142 Views
Last Modified: 2014-12-16
Hi Experts,
I would like to replace the year part in a date in SQL Server.
I've tried the following, but I get this error message:
Msg 242, Level 16, State 3, Line 1
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.

UPDATE VacationBid
   SET Day1 = REPLACE(Day1,'2014','1015')
 WHERE Day1 LIKE '%2014%'

Open in new window


Thank you for your help
0
Comment
Question by:romsom
[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
8 Comments
 
LVL 66

Assisted Solution

by:Jim Horn
Jim Horn earned 200 total points
ID: 40503061
Perhaps the medievial cultures in the year 1015 do not wish you to change dates.

Works on my 2008R2 box, assuming that the column you're trying to change is a date:
CREATE TABLE #tmp (dt date) 

INSERT INTO #tmp (dt) 
VALUES ('2014-01-01'), ('2014-03-01'), ('2010-01-07'), ('2009-01-01')

UPDATE #tmp 
SET dt = REPLACE(dt, '2014', '2015') 
WHERE dt LIKE '%2014%'

SELECT * FROM #tmp

Open in new window


Perhaps it's a leap year issue, as the below returns a type conversion error
SELECT CAST('1015-02-29' as date) 

Open in new window

0
 
LVL 18

Assisted Solution

by:Simon
Simon earned 150 total points
ID: 40503070
update vacationBid
set day1=dateadd(yy,1,day1)
where year(day1)=2014
0
 
LVL 22

Accepted Solution

by:
plusone3055 earned 150 total points
ID: 40503073
you really want the year to read 1015 ???

or was that a typo ?
i think your looking for 2015

UPDATE VacationBid
   SET Day1 = DATEADD(YEAR,+1,Day1)
 WHERE Day1(YEAR) LIKE '%2014%'
0
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 

Author Comment

by:romsom
ID: 40503090
Very funny!
My users didn't want to travel back in time just to take vacation in year 1015.
Sorry, it was just a typo.
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40503118
We aim to please.

Every once in awhile we get a question about Access 200, and it takes awhile to explain that the Romans' first couple of desktop database applications left a lot to be desired, and that 202 was really the first usable application.
0
 
LVL 22

Expert Comment

by:plusone3055
ID: 40503127
JH

I liked the old school overalls pic .. why so formal now ?
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40503210
When you blow two grand on family photos there's a strong desire to justify spending the money.

The previous photo was after a Tough Mudder I ran a couple of years ago.
If you think it's a better photo for EE then I'll put it back.  
You're the first one that's commented on it.
0
 
LVL 22

Expert Comment

by:plusone3055
ID: 40503260
I hear that
that's why all family photos are taken with our cells :)

coming from the guy DJ'ing in his picture so what do I know :)
0

Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

689 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