代码之家  ›  专栏  ›  技术社区  ›  David Brierton

SQL Server:将秒转换为小时

  •  0
  • David Brierton  · 技术社区  · 6 年前

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

    这可以在飞行中进行吗?

    我尝试添加convert并将其命名为timeinhours,但无法使convert正常工作。

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

    1 回复  |  直到 6 年前
        1
  •  4
  •   Sean Lange    6 年前

    您需要除以3600,但要小心避免整数除法。把0加到除数上。

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