如何将数值作为秒添加到日期时间字段并在新列中报告数据

时间:2015-02-04 22:15:42

标签: sql datetime sql-server-2005

我需要将ElapsedTime字段转换为小时/分钟/秒并将其添加到creationtime字段,并将结果报告到我将调用EndTime的新列中。这是我必须收集数据的查询:

select 
ElapsedTime, ChannelUsed, documents.creationtime 
from 
historytrx (nolock) inner join 
history on 
historytrx.handle = history.handle inner join 
documents on 
history.owner = documents.handle inner join 
DocFiles on 
documents.docfiledba = docfiles.handle 
where 
creationtime > '2015-02-02 20:00:00.000' and 
creationtime < '2015-02-02 20:01:00.000'  and 
RemoteServer = 'DMG4120-01668' and 
ElapsedTime != '0'

这是当前的输出:

ElapsedTime   ChannelUsed    creationtime
1042          1              2015-02-02 20:00:03.000
27            35             2015-02-02 20:00:05.000
57            50             2015-02-02 20:00:05.000

这是我想要的输出:

ElapsedTime   ChannelUsed    creationtime             EndTime
1042          1              2015-02-02 20:00:03.000  2015-02-02 20:17:39.000
27            35             2015-02-02 20:00:05.000  2015-02-02 20:00:32.000
57            50             2015-02-02 20:00:05.000  2015-02-02 20:01:03.000

感谢所有人提前获得任何帮助。

2 个答案:

答案 0 :(得分:0)

尝试:

select 
 ElapsedTime, 
 ChannelUsed, 
 creationtime, 
 convert(datetime, dateadd(ss,elapsedtime,creationtime), 121) as endTime
from 
 historytrx t inner join 
 history h on 
 t.handle = h.handle inner join 
 documents d on 
 h.owner = d.handle inner join 
 DocFiles f on 
 d.docfiledba = f.handle 
where 
 creationtime > '2015-02-02 20:00:00.000' and 
 creationtime < '2015-02-02 20:01:00.000'  and 
 RemoteServer = 'DMG4120-01668' and 
 ElapsedTime != '0'

请参阅this

HTH

答案 1 :(得分:0)

试试这个:

  select 
    ElapsedTime, ChannelUsed, documents.creationtime,
    dateAdd(ss,ElapsedTime,documents.creationtime) as EndTime
    from 
    historytrx (nolock) inner join 
    history on 
    historytrx.handle = history.handle inner join 
    documents on 
    history.owner = documents.handle inner join 
    DocFiles on 
    documents.docfiledba = docfiles.handle 
    where 
    creationtime > '2015-02-02 20:00:00.000' and 
    creationtime < '2015-02-02 20:01:00.000'  and 
    RemoteServer = 'DMG4120-01668' and 
    ElapsedTime != '0'

DateAdd()函数有3个参数。 ss代表秒,第二个参数是要添加的秒数,第三个参数是添加秒值的起始日期。