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



我需要将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

提前感谢大家的帮助。

尝试:

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'

看到这个

HTH

试试这个:

  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代表秒,第二个参数是要添加的秒数,第三个参数是将秒值添加到的开始日期。

最新更新