【问题标题】:SQL Server : convert seconds into hoursSQL Server:将秒转换为小时
【发布时间】:2019-01-24 03:38:38
【问题描述】:

我正在使用 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

这可以即时进行吗?

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

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

【问题讨论】:

标签: sql-server


【解决方案1】:

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

declare @Seconds int = 27900

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

【讨论】:

  • 这也完美地演示了创建一个Minimal 示例,该示例演示了问题并避免了在不相关时必须理解大型复杂查询。
  • 使用列名而不是变量。
  • 当然是......你没有仔细阅读我的帖子。您必须包含 .0 以强制您的文字为小数。否则,您将被整数除。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-31
  • 2021-10-29
  • 1970-01-01
  • 2011-06-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多