Solved

SQL Update Query - Change date ONLY in time/date field

Posted on 2014-08-14
3
984 Views
Last Modified: 2014-08-14
I need to change a bunch of rows that was dated 08/30/2014 to 05/31/2014 but I don't want to change the time stamp within the value.  So if the row shows 08/30/2014 00:30:27.513...I want the row to be updated to 05/31/2014 00:30:27.513
0
Comment
Question by:Lee R Liddick Jr
3 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40260792
Hmm.  Very wild guess..
UPDATE your_table
SET the_datetime = CAST('2014-05-31 ' + CAST(the_datetime as time) as datetime)
WHERE CAST(the_datetime as date) = '2014-08-30'

Open in new window

>a bunch of rows that was dated 08/30/2014 to 05/31/2014
>So if the row shows 08/30/2014
Define this a little further for us please.
0
 
LVL 46

Accepted Solution

by:
Vitor Montalvão earned 500 total points
ID: 40260798
Try this:

UPDATE TableName
SET MyDate = DATEADD(days, DATEDIFF(day,'08/30/2014','05/31/2014') , MyDate)
WHERE MyDate BETWEEN '08/30/2014' AND '08/31/2014'

Open in new window

0
 

Author Closing Comment

by:Lee R Liddick Jr
ID: 40260874
That was it...thanks!
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

863 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

26 Experts available now in Live!

Get 1:1 Help Now