如何在同一天添加日期范围?如何调用函数将秒转换为hh:mm:ss [英] How to add date range for the same day? How to call function to convert seconds to hh:mm:ss

查看:184
本文介绍了如何在同一天添加日期范围?如何调用函数将秒转换为hh:mm:ss的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SQL Server 2014上编写代码来为某一天提取记录。然后,我将使用Report Builder创建报告。



我不确定我的代码是否正确。从上午7:45到下午6:15,班次的时间为'yyyy-mm-dd hh:mm:sss'格式。我需要统计每天那段时间的总电话(和其他字段)。我还需要提供一个参数,以便观众可以选择报告的日期范围。



我创建了一个函数将总秒数转换为HH:MM:SS格式。该函数的代码是:

$ $ p $ code $ CREATE FUNCTION dbo.UDF_SecondsToHMS(@Seconds int)RETURNS表AS
RETURN(
SELECT CONVERT(VARCHAR(20),@Seconds / 3600)+':'
+ RIGHT('00'+ CONVERT(VARCHAR(2),(@Seconds%3600)/ 60),2 )+':'
+ RIGHT('00'+ CONVERT(VARCHAR(2),@秒%60),2)AS [HMS]);

我试过的代码是:

<$ SUM(CallsAnswerTotal)/ 600)[ASA] - >我怎样在这里调用这个函数呢?
-----------------------(我在这里调用函数)
,(SELECT
(SELECT HMS
FROM [UDF_SecondsToHMS](SUM(DATEDIFF(second,AgentLoginTime,AgentLogoutTime))))[HMS]
FROM [AgentStats]
WHERE DATEADD(dd,0,DATEDIFF(dd,0,StartDate) )= CAST('2017-03-08'AS DATETIME)
)[实际班次小时数]
--------------------- - (函数结束,我在这里帮助调用了这个函数。)
,(SUM(ACDDuration)/ 600) ACD时间] - >我怎样在这里调用这个函数呢?

FROM [CallsByPeriodStats]

WHERE DateOfCalls> = @Report_Date - 日期显示为'2017-02-22 00:00:000'
AND DateOfCalls< @Report_Date +本月的第一天 - 通常应该是'2017-02-23 00:00:000'
- 但是如果@Report_Date是月的最后一天,该怎么办?
GROUP BY CAST [StartDate as DATE]




  1. 我将我创建的函数称为在上面指出的2个位置将秒转换为hh:mm:ss的函数?

  2. 如果我给出一个日期范围,如果日期是该月的最后一个日期,它将如何处理?


解决方案

你的函数当前返回一个表。我不确定这是否是必需的,但您可能需要一个函数,它只返回表示HMS中持续时间的字符串,或者表示HMS中持续时间的Time值。



创建另一个函数。

  CREATE FUNCTION dbo.UDF_SecondsToHMSVarChar(@Seconds int)RETURNS varchar(20)AS 
BEGIN
RETURN(
SELECT CONVERT(VARCHAR(20),@Seconds / 3600)+':'
+ RIGHT('00'+ CONVERT(VARCHAR(2),( ('00'+ CONVERT(VARCHAR(2),@秒%60),2)AS [HMS]
)+':'
+
END

这样会给你回文字,所以你将无法任何计算直接在它上面,如果你只是需要它在报告上显示'原样',这将是很好的。



要使用该函数,只需要替换当前返回秒和函数和原始的expr ession作为参数传递,例如

 ,(SUM(TimeToAnswerTotal)/ 600)[ASA] 

会变成

 ,dbo。 UDF_SecondsToHMSVarChar((SUM(TimeToAnswerTotal)/ 600))[ASA] 

只需将两个日期时间值转换为日期类型即可。基本上这只给你日期时间的日期部分,所以你可以直接与传入日期进行比较。

  WHERE DateOfCalls> ; = @Report_Date 

变成

  WHERE CAST(DateOfCalls AS DATE)= CAST(@Report_Date AS DATE)


I'm writing code on SQL Server 2014 to pull records for a single day. I will then be using Report Builder to create the report.

I'm not sure if my code is right. The times of the shifts are in 'yyyy-mm-dd hh:mm:sss' format starting at 7:45am to 6:15pm. I need to count total calls (and other fields) for that time period per day. I will also need to give a parameter so the viewer can select the date range for the report.

I created a function to convert total seconds to HH:MM:SS format. The code for the function is:

CREATE FUNCTION dbo.UDF_SecondsToHMS(@Seconds int) RETURNS table AS
RETURN (
         SELECT CONVERT(VARCHAR(20), @Seconds / 3600) + ':' 
       + RIGHT('00' + CONVERT(VARCHAR(2), (@Seconds % 3600) / 60) ,2) + ':' 
       + RIGHT('00' + CONVERT(VARCHAR(2), @Seconds%60), 2) AS [HMS] );

The code I have tried is:

SELECT 
     SUM(CallsAnswered) CallsAnswered
    ,SUM(TotalCalls) TotalCalls
    ,(SUM(TimeToAnswerTotal) / 600) [ASA]   --> How do I call the function here too?
----------------------- (I called the function here)
    ,(SELECT 
        (SELECT HMS 
         FROM [UDF_SecondsToHMS](SUM (DATEDIFF(second, AgentLoginTime, AgentLogoutTime)))) [HMS]
      FROM [AgentStats]
      WHERE DATEADD(dd, 0, DATEDIFF(dd, 0, StartDate)) = CAST('2017-03-08' AS DATETIME)
      ) [Actual Shift Hrs.]
----------------------- (End of the function here. I had help calling this function here.)
    ,(SUM(ACDDuration) / 600) [Avg. ACD Time]  --> How do I call the function here too?

FROM [CallsByPeriodStats]

WHERE DateOfCalls >= @Report_Date   --typically the date shows as '2017-02-22 00:00:000'
  AND DateOfCalls <  @Report_Date + 1 day of the month  --typically it should be '2017-02-23 00:00:000'
                              --but what if @Report_Date is last day of month?
GROUP BY CAST[StartDate as DATE]

  1. How do I call the function I created to convert seconds to hh:mm:ss in the 2 places I indicated above?
  2. If I give a date range, how will it take it if the date is the last date of the month?

解决方案

Your function currently returns a table. I'm not sure if that is required but you will probably need a function that just returns either a string representing the duration in HMS or a Time value representing the duration in HMS

Create another function something like this..

CREATE FUNCTION dbo.UDF_SecondsToHMSVarChar(@Seconds int) RETURNS varchar(20) AS
BEGIN
    RETURN (
             SELECT CONVERT(VARCHAR(20), @Seconds / 3600) + ':' 
           + RIGHT('00' + CONVERT(VARCHAR(2), (@Seconds % 3600) / 60) ,2) + ':' 
           + RIGHT('00' + CONVERT(VARCHAR(2), @Seconds%60), 2) AS [HMS] 
           )
END

This will just give you text back, so you won't be able to any calculation directly on it, this will be fine if you just need it showing 'as-is' on a report.

To use the function just replace anything that currently is returning seconds with the function and the original expression passed as the parameter e.g.

,(SUM(TimeToAnswerTotal) / 600) [ASA]

would become

, dbo.UDF_SecondsToHMSVarChar((SUM(TimeToAnswerTotal) / 600)) [ASA]

To select only data from date, simply cast the two datetime values to date types when you compare them. Basically this give you only the date portion of the datetime so you can compare it directly with the passed in date.

WHERE DateOfCalls >= @Report_Date

becomes

WHERE CAST(DateOfCalls AS DATE) = CAST(@Report_Date AS DATE)

这篇关于如何在同一天添加日期范围?如何调用函数将秒转换为hh:mm:ss的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆