Solved

How to replace the year in a date in SQL Server

Posted on 2014-12-16
8
126 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:SimonAdept
SimonAdept 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now