Solved

ms sql + update datetime entry

Posted on 2011-03-14
4
296 Views
Last Modified: 2012-06-27
I have a bunch of dates that entered in a table incorrectly, for March 11th, 2011, it entered 11/3/2011, is there a way to update these entries with and update statement, but only changing the date part and keeping the time part the same, as each entry has a time as well as the date.
0
Comment
Question by:dkilby
[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
  • 2
  • 2
4 Comments
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 35130303
If you ONLY want to take the entries where the date portion is 2011-11-03 and cast them back to 2011-03-11...



CREATE TABLE #foo (Dt datetime)

INSERT INTO #foo (Dt) VALUES ('2011-01-15 11:22:33')
INSERT INTO #foo (Dt) VALUES ('2011-03-10 19:47:01')
INSERT INTO #foo (Dt) VALUES ('2011-07-15 11:22:33')
INSERT INTO #foo (Dt) VALUES ('2011-11-03 06:30:00')

SELECT Dt FROM #foo ORDER BY Dt

UPDATE #foo
SET Dt = DATEADD(day, DATEDIFF(day, '2011-11-03', '2011-03-11'), Dt)
WHERE Dt >= '2011-11-03' AND Dt < '2011-11-04'

SELECT Dt FROM #foo ORDER BY Dt

DROP TABLE #foo

Open in new window

0
 

Author Comment

by:dkilby
ID: 35130866
I have a lot of rows, do i have to insert each row into the temp table one row at a time?
0
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 35131002
No, that was just a demonstration.  You can simply use this, substituting the actual table/column names:

UPDATE [TableName]
SET [DateColumn] = DATEADD(day, DATEDIFF(day, '2011-11-03', '2011-03-11'), [DateColumn])
WHERE [DateColumn] >= '2011-11-03' AND [DateColumn] < '2011-11-04'

Open in new window

0
 

Author Closing Comment

by:dkilby
ID: 35131196
thank you
0

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

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…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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.

737 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