updating only date part from datetime in sql server 2000
I have data in the table like the following.
col1 col2 col3
----------------开发者_StackOverflow中文版----------------------------------------
6/5/2010 18:05:00 6/2/2010 10:05:00 Null
6/8/2010 15:05:00 6/3/2010 10:45:00 6/5/2010 11:05:00
6/3/2010 15:05:00 Null 6/7/2010 12:05:00
6/1/2010 15:05:00 6/3/2010 10:45:00 6/1/2010 14:05:00
what my requirement is I want to update the date of there columns with single date without disturbing the time. say for example I want to update the table data with 6/1/2010 where the field data is not null. please let me know the query for updating the table data.
thanks & regards,
murali
I think this should work for you.
create table #t
(
col1 datetime
)
Insert Into #t
values ('2010-06-01 10:00:00')
Insert Into #t
values ('2010-06-06 11:00:00')
Insert Into #t
values ('2010-05-24 12:40:00')
Insert Into #t
values ('2010-05-07 13:00:00')
Insert Into #t
values (Null)
declare @newDate datetime
set @newDate = '2010-07-01'
update #t
Set col1 = DateAdd(day, DateDiff(day, col1, @newDate), Col1)
Where Col1 is not null
select * From #t
drop table #t
You may need to do it via SELECT statement as you dont need to run UPDATE statement each time new data are added to the table
精彩评论