Solved

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

Posted on 2014-08-14
3
1,025 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 48

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Requesting help with creating an SQL query with 2 tables 6 27
SQL USE DATABASE VARIABLE 5 29
SQL Quer 4 22
SQL Availablity Groups List 2 8
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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

837 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