【问题标题】:how to sum time in sql server 2008?如何在 sql server 2008 中求和时间?
【发布时间】:2014-02-17 14:44:56
【问题描述】:

如何对sql server中的时间列求和

我想总结周一、周二、周三、周四、周五和周六的时间并更新到 Tot_hrs 我有更新逻辑我无法总结周一到周六的时间

CREATE TABLE WEEKLY_ATTN(
[SYSID] [int] IDENTITY(1,1) NOT NULL,
[EMPLOYEE_SYSID] [int] NULL,
[Mon_day] [varchar](10) NULL,
[Tue_day] [varchar](10) NULL,
[Wed_day] [varchar](10) NULL,
[Thu_day] [varchar](10) NULL,
[Fri_day] [varchar](10) NULL,
[Sat_day] [varchar](10) NULL,
[Tot_Hrs] [varchar](20) NULL

);

INSERT INTO WEEKLY_ATTN (employee_sysid,mon_day,tue_day,wed_day,thu_day,fri_day,sat_day)
values(41,'11:01','11:57','11:02','10:19','09:26',null);

下面是我用过的查询

select mon_day,Tue_day,wed_day,thu_day,fri_Day,sat_day,
cast(dateadd(s,
isnull(datediff(s, 0, mon_day), 0)+ 
isnull(datediff(s, 0, Tue_day), 0)+ 
isnull(datediff(s, 0, wed_day), 0)+
isnull(datediff(s, 0, Thu_day), 0)+
isnull(datediff(s, 0, fri_day), 0)+
isnull(datediff(s, 0, sat_day), 0)
,0) as time(0)) as Tot_hrs
from weekly_attn where employee_sysid=41; 

mon_day Tue_day wed_day thu_day fri_Day  sat_day tot_hrs
---------------------------------------------------------
11:01    11:57  11:02   10:19   09:26    NULL    05:45:00  

但是总小时数是 53:45 小时,任何人都可以纠正我的错误。有什么帮助吗?

【问题讨论】:

  • 我通过以下方式得到了答案
  • 嗨。哪一种是“跟随方式”?
  • 我通过以下方式得到了答案 set @V_TOTAL_HRS=(select isnull(datediff(s, 0, mon_day), 0)+ isnull(datediff(s, 0, Tue_day), 0)+ isnull(datediff(s, 0, wed_day), 0)+ isnull(datediff(s, 0, Thu_day), 0)+ isnull(datediff(s, 0, fri_day), 0) from week_attn where employee_sysid=@V_EMPID) - -select @V_TOTAL_HRS=cast(@V_TOTAL_HRS/3600 as varchar)+':'+cast((@V_TOTAL_HRS%3600)/60 as varchar) -- 53:45
  • 干得好兄弟.. 继续努力:)

标签: sql-server sql-server-2008-r2


【解决方案1】:

varchar 应该是时间。

select *,CONVERT(varchar, DATEADD(ms,Tot_hrs * 1000, 0),114) from 
(select mon_day,Tue_day,wed_day,thu_day,fri_Day,sat_day,
isnull(cast(datediff(s, 0,cast( mon_day as time)) as float),0)+ 
isnull(cast(datediff(s, 0,cast(  Tue_day as time))as float),0)+ 
isnull(cast(datediff(s, 0,cast(  wed_day as time))as float),0)+
isnull(cast(datediff(s, 0,cast(  Thu_day as time))as float),0)+
isnull(cast(datediff(s, 0,cast( fri_day as time))as float),0)+
isnull(cast(datediff(s, 0,cast( sat_day as time)) as float),0)
as Tot_hrs
from @weekly_attn where employee_sysid=41)tbl

【讨论】:

  • SELECT CONVERT(varchar (5), DATEADD(ms,Tot_hrs * 1000, 0),114) 给了我正确的格式,谢谢
  • 如果小时数超过 24 小时,我的逻辑工作正常
【解决方案2】:

以下是一种总结时间的方式

select 
CAST(
    CAST(SUBSTRING(isnull(mon_day,0),1,2) as int)+CAST(SUBSTRING(isnull(Tue_day,0),1,2) as int)+
    CAST(SUBSTRING(isnull(wed_day,0),1,2) as int)+CAST(SUBSTRING(isnull(Thu_day,0),1,2) as int)+
    CAST(SUBSTRING(isnull(fri_day,0),1,2) as int)+CAST(SUBSTRING(isnull(sat_day,0),1,2) as int) 
    +   
    (
    CAST(SUBSTRING(isnull(mon_day,0),4,5) as int)+CAST(SUBSTRING(isnull(Tue_day,0),4,5) as int)+
    CAST(SUBSTRING(isnull(wed_day,0),4,5) as int)+CAST(SUBSTRING(isnull(Thu_day,0),4,5) as int)+
    CAST(SUBSTRING(isnull(fri_day,0),4,5) as int)+CAST(SUBSTRING(isnull(sat_day,0),4,5) as int) 
    )/60

AS varchar(50))
+':'+
CAST(
    (
    CAST(SUBSTRING(isnull(mon_day,0),4,5) as int)+CAST(SUBSTRING(isnull(Tue_day,0),4,5) as int)+
    CAST(SUBSTRING(isnull(wed_day,0),4,5) as int)+CAST(SUBSTRING(isnull(Thu_day,0),4,5) as int)+
    CAST(SUBSTRING(isnull(fri_day,0),4,5) as int)+CAST(SUBSTRING(isnull(sat_day,0),4,5) as int) 
    )%60
AS varchar(50))
from weekly_attn

输出:53:45

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-18
    • 1970-01-01
    • 2011-03-22
    • 1970-01-01
    • 1970-01-01
    • 2011-03-19
    相关资源
    最近更新 更多