Unix Technical Forum

how to update a datetime field

This is a discussion on how to update a datetime field within the SQL Server forums, part of the Microsoft SQL Server category; --> How can I update only the date portion of a datetime field? Example: datetime field = '3/12/1995 12:05:50 PM' ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 05:33 AM
RDRaider
 
Posts: n/a
Default how to update a datetime field

How can I update only the date portion of a datetime field?
Example:
datetime field = '3/12/1995 12:05:50 PM'

How can I change just the day '12' to a '7'

Thanks


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 05:33 AM
Hugo Kornelis
 
Posts: n/a
Default Re: how to update a datetime field

On Tue, 11 Jan 2005 20:56:17 GMT, RDRaider wrote:

>How can I update only the date portion of a datetime field?
>Example:
>datetime field = '3/12/1995 12:05:50 PM'
>
>How can I change just the day '12' to a '7'
>
>Thanks


Hi RDRaider,

You're using an ambiguous date format. Is it december 3rd or March 12th?

DECLARE @dt datetime
SET @dt = '1995-12-03T12:05:50'
SELECT DATEADD(month, 7 - month(@dt), @dt)

-- Or did you mean this?
SET @dt = '1995-03-12T12:05:50'
SELECT DATEADD(day, 7 - day(@dt), @dt)

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 05:33 AM
RDRaider
 
Posts: n/a
Default Re: how to update a datetime field

Thanks for the reply. What I really need is to change a March 12, 1995 to
March 7, 1995. But I don't want to change the time. So I have over 1500
records with the same 3/12/1995 but with different times. I need to change
the date to 3/7/1995 but retain the time field which is different for each
record.

RD

"Hugo Kornelis" <hugo@pe_NO_rFact.in_SPAM_fo> wrote in message
news:v4h8u0hqua5athebnjnfsfc873kdp7vec3@4ax.com...
> On Tue, 11 Jan 2005 20:56:17 GMT, RDRaider wrote:
>
>>How can I update only the date portion of a datetime field?
>>Example:
>>datetime field = '3/12/1995 12:05:50 PM'
>>
>>How can I change just the day '12' to a '7'
>>
>>Thanks

>
> Hi RDRaider,
>
> You're using an ambiguous date format. Is it december 3rd or March 12th?
>
> DECLARE @dt datetime
> SET @dt = '1995-12-03T12:05:50'
> SELECT DATEADD(month, 7 - month(@dt), @dt)
>
> -- Or did you mean this?
> SET @dt = '1995-03-12T12:05:50'
> SELECT DATEADD(day, 7 - day(@dt), @dt)
>
> Best, Hugo
> --
>
> (Remove _NO_ and _SPAM_ to get my e-mail address)



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 05:33 AM
Erland Sommarskog
 
Posts: n/a
Default Re: how to update a datetime field

RDRaider (rdraider@sbcglobal.net) writes:
> Thanks for the reply. What I really need is to change a March 12, 1995
> to March 7, 1995. But I don't want to change the time. So I have over
> 1500 records with the same 3/12/1995 but with different times. I need
> to change the date to 3/7/1995 but retain the time field which is
> different for each record.


UPDATE tbl
SET field = dateadd(DAY, -5, field)
FROM tbl
WHERE field >= '19950312' AND field < '19950313'



--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-29-2008, 05:33 AM
RDRaider
 
Posts: n/a
Default Re: how to update a datetime field

Thanks, that did the trick.

"Erland Sommarskog" <esquel@sommarskog.se> wrote in message
news:Xns95DC1F854793Yazorman@127.0.0.1...
> RDRaider (rdraider@sbcglobal.net) writes:
>> Thanks for the reply. What I really need is to change a March 12, 1995
>> to March 7, 1995. But I don't want to change the time. So I have over
>> 1500 records with the same 3/12/1995 but with different times. I need
>> to change the date to 3/7/1995 but retain the time field which is
>> different for each record.

>
> UPDATE tbl
> SET field = dateadd(DAY, -5, field)
> FROM tbl
> WHERE field >= '19950312' AND field < '19950313'
>
>
>
> --
> Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se
>
> Books Online for SQL Server SP3 at
> http://www.microsoft.com/sql/techinf...2000/books.asp



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:52 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com