SQL Server:将秒转换为小时

时间:2019-01-23 19:21:22

标签: sql-server

我正在使用SQL Server,并使用内部联接或左外部联接将大约10个表联接在一起。

我选择的vp_timesheetpunch.TIMEINSECONDS中有一列(以秒为单位的时间),以秒为单位,我想在说了多少小时后添加另一列。这样就可以同时列出秒和小时。

select
    vp_timesheetpunch.personnum [Assoc ID],
    vp_timesheetpunch.personfullname [Assoc Name],
    vp_timesheetpunch.laborlevelname4 [Department],
    vp_timesheetpunch.eventdate [Shift Date],
    shiftassignmnt.shiftstartdate [Scheduled Start],
    vp_timesheetpunch.startdtm [Rounded Start],
    vp_timesheetpunch.inpunchdtm [Actual Start],
    vp_timesheetpunch.enddtm [Rounded End],
    vp_timesheetpunch.outpunchdtm [Actual End],
    vp_timesheetpunch.TIMEINSECONDS [Time in seconds]
from
    vp_timesheetpunch
left outer join
    vp_punchexceptions on vp_timesheetpunch.timesheetitemid = vp_punchexceptions.timesheetitemid
inner join
    timesheetitem on vp_timesheetpunch.timesheetitemid = timesheetitem.timesheetitemid
inner join
    workedshift on timesheetitem.workedshiftid = workedshift.workedshiftid
inner join
    shfasgnwshfmm on workedshift.workedshiftid = shfasgnwshfmm.workedshiftid
inner join
    shiftassignmnt on shfasgnwshfmm.shiftassignid = shiftassignmnt.shiftassignid
where
    --limit rows to the specified pay period
    vp_timesheetpunch.eventdate = '1/22/2019'
    --exclude rows that are missing data
    and vp_timesheetpunch.inpunchdtm is not null
    and vp_timesheetpunch.outpunchdtm is not null
    --limit rows to shifts with exceptions
order by
    vp_timesheetpunch.personnum,
    vp_timesheetpunch.eventdate

这有可能做到吗?

我尝试添加转换并命名为Timeinhours,但无法使转换正常工作。

数据以秒为单位列出时间,例如“ 27900”

1 个答案:

答案 0 :(得分:4)

您需要除以3600,但需要注意避免整数除。只需将.0添加到除数即可。

declare @Seconds int = 27900

select [hours] =  convert(decimal(7,2), @Seconds / 3600.0)