日期:2014-05-18  浏览次数:20569 次

SQL 更新时间字段问题
一个表tbTimesheet中有两列WorkDate和StartTime,都是时间类型,现在要把WorkDate中的年,月,日更新到StartTime中,小时和分钟不变,求一个SQL,大家帮忙啊~~~~~

例:
tbTimesheet:
源数据:
Id WorkDate StartTime
1 2012-02-12 00:00:00.000 2012-02-16 08:00:00.000
2 2012-03-13 00:00:00.000 2012-03-16 08:00:00.000

更新后想得到:
Id WorkDate StartTime
1 2012-02-12 00:00:00.000 2012-02-12 08:00:00.000
2 2012-03-13 00:00:00.000 2012-03-13 08:00:00.000


------解决方案--------------------
SQL code

create table tbTimesheet
(Id int, WorkDate datetime, StartTime datetime)

insert into tbTimesheet
select 1, '2012-02-12 00:00:00.000', '2012-02-16 08:00:00.000' union all
select 2, '2012-03-13 00:00:00.000', '2012-03-16 08:00:00.000'


update tbTimesheet
set StartTime=convert(varchar,WorkDate,23)+' '+convert(varchar,StartTime,108)

select * from tbTimesheet

Id          WorkDate                StartTime
----------- ----------------------- -----------------------
1           2012-02-12 00:00:00.000 2012-02-12 08:00:00.000
2           2012-03-13 00:00:00.000 2012-03-13 08:00:00.000

(2 row(s) affected)

------解决方案--------------------
SQL code
create table tbTimesheet(Id int,WorkDate datetime,StartTime datetime)
insert into tbTimesheet values(1, '2012-02-12 00:00:00.000', '2012-02-16 08:00:00.000')
insert into tbTimesheet values(2, '2012-03-13 00:00:00.000', '2012-03-16 08:00:00.000')

update tbTimesheet  
set StartTime = convert(varchar(10),WorkDate,120) + ' ' + convert(varchar(8),StartTime,108)
 
select * From tbTimesheet 

drop table  tbTimesheet 

/*
Id          WorkDate                                               StartTime                                              
----------- ------------------------------------------------------ ------------------------------------------------------ 
1           2012-02-12 00:00:00.000                                2012-02-12 08:00:00.000
2           2012-03-13 00:00:00.000                                2012-03-13 08:00:00.000

(所影响的行数为 2 行)

*/