Solved

How to replace the year in a date in SQL Server

Posted on 2014-12-16
8
132 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
8 Comments
 
LVL 65

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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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 65

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 65

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

777 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