?
Solved

Update Month,Day,Year in SQL Datetime field

Posted on 2011-03-21
12
Medium Priority
?
727 Views
Last Modified: 2012-05-11
Hello Experts,

How can I update Month,Day and Year in Datetime field , I'm using SQL server 2005.

Some weird datetime record entry in database messed up my reports, the time entry is correct.But date is wrong.

I got this '2100-01-01 10:05:00', it needs to be '2011-03-21 10:05:00'
0
Comment
Question by:ASPDEV
  • 4
  • 4
  • 4
12 Comments
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35181711

update table1
set datefield = '2011-03-21 10:05:00'
where datefield = '2100-01-01 10:05:00'
0
 

Author Comment

by:ASPDEV
ID: 35181719
But I have nearly 100's of them.
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 35181732
Are all the dates set to the same or are they all just very much incorrect?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:ASPDEV
ID: 35181748
Most of them are set to year 2100 and 2099.
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 35181765
And they all need to be set to the 21st March 2011?
0
 

Author Comment

by:ASPDEV
ID: 35181791
Correct.
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35181806


update table1
set datefield = '2011-03-21' + ' ' + CONVERT(varchar(8), @mydate, 114)
where CONVERT(varchar(10), datefile, 120) = '2100-01-01'
or CONVERT(varchar(10), datefile, 120) = '2099-01-01'
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 35181812
update mytable set mydate = dateadd(dd, DATEDIFF(dd, mydate, '2011-03-21'), mydate)
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 35181826
Mine needs testing :) Not in front of SQL Management Studio to test!
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35181843
Did'nt clean up properly
replace @mydate with datefield

update table1
set datefield = '2011-03-21' + ' ' + CONVERT(varchar(8), datefield, 114)
where CONVERT(varchar(10), datefile, 120) = '2100-01-01'
or CONVERT(varchar(10), datefile, 120) = '2099-01-01'
0
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 2000 total points
ID: 35181858
Sorry, my typing is out of whack

update table1
set datefield = '2011-03-21' + ' ' + CONVERT(varchar(8), datefield, 114)
where CONVERT(varchar(10), datefield, 120) = '2100-01-01'
or CONVERT(varchar(10), datefield, 120) = '2099-01-01
0
 

Author Closing Comment

by:ASPDEV
ID: 35182115
Thanks.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard 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.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

850 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