日期:2014-05-19  浏览次数:20613 次

请教关于日期的SQL语句
create   table   test
(
    派工项目     varchar(10),
    派工日期     datetime,
    工时定额     decimal(6,2),
    规定完工时间     datetime
)

insert   into   test   select   'aaa ', '2007-05-29   20:30 ',3.30,null
insert   into   test   select   'bbb ', '2007-05-29   20:32 ',1.00,null

select   *   from   test
drop   table   test
/*
怎样才能得到这样的结果
派工项目                 派工日期                         工时定额           规定完工时间
aaa                   2007-05-29   20:30               3.30             2007-05-29   0:00
bbb                   2007-05-29   20:32               1.00             2007-05-29   21:32
*/

------解决方案--------------------
insert into test select 'aaa ', '2007-05-29 20:30 ',3.30,null
insert into test select 'bbb ', '2007-05-29 20:32 ',1.00,null

select * from test
drop table test
/*
怎样才能得到这样的结果
派工项目 派工日期 工时定额 规定完工时间
aaa 2007-05-29 20:30 3.30 2007-05-29 0:00
bbb 2007-05-29 20:32 1.00 2007-05-29 21:32
*/


select 派工项目,派工日期=convert(char(19),派工日期,120),工时定额,规定完工时间=convert(char(19),dateadd(hour,convert(datetime,工时定额),派工日期),120)
from test

派工项目 派工日期 工时定额 规定完工时间
---------- ------------------- -------- -------------------
aaa 2007-05-29 20:30:00 3.30 2007-05-30 00:00:00
bbb 2007-05-29 20:32:00 1.00 2007-05-29 21:32:00

------解决方案--------------------
select 派工项目,派工日期=convert(char(19),派工日期,120),
工时定额,
规定完工时间=convert(char(19),dateadd(hour,cast(工时定额 as int),
dateadd(minute,cast(right(工时定额,len(工时定额)-charindex( '. ',工时定额)) as int),派工日期)),120)
from test
------解决方案--------------------
select 派工项目,派工日期=convert(char(20),派工日期,120),工时定额,规定完工时间=convert(char(20),dateadd(hour,工时定额,派工日期),120)
from test