0

我需要 1 天的字符串值

“23:59:59”正在运行,但会导致延迟

“24:00:00”不起作用:将数据类型 varchar 转换为 datetime 时出错。

“1.00:00:00”不起作用:将数据类型 varchar 转换为 datetime 时出错。

这是我的代码:

 PROCEDURE [dbo].[Procedure1] 

    @Start datetime, 
    @Finish datetime,
    @TimeRange datetime
AS
BEGIN

    SET NOCOUNT ON;

    declare @TimeRanges as TABLE (SessionStart datetime, SessionEnd datetime);

     with TimeRanges as (
  select @Start as StartTime, @Start + @TimeRange as EndTime
  union all
  select StartTime + @TimeRange, EndTime + @TimeRange
    from TimeRanges
    where EndTime < @Finish )
  select StartTime, EndTime, Count( Test.ScenarioID ) as TotalPeaks
    from TimeRanges as TR left outer join
      dbo.Test as Test on TR.StartTime <= Test.SessionStartTime and Test.SessionCloseTime < TR.EndTime
    group by TR.StartTime, TR.EndTime   
END
4

1 回答 1

1

您正在尝试将该datetime类型用作不适合您的时间跨度类型。

我建议您将 timerange 参数更改为整数。然后指定它是秒、分钟、小时、天或其他。

如果你决定让它几天:

PROCEDURE [dbo].[Procedure1] 

    @Start datetime, 
    @Finish datetime,
    @TimeRange int
AS
BEGIN

    SET NOCOUNT ON;

    declare @TimeRanges as TABLE (SessionStart datetime, SessionEnd datetime);

     with TimeRanges as (
  select @Start as StartTime, DATEADD(day, @TimeRange, @Start) as EndTime
  union all
  select DATEADD(day, @TimeRange, StartTime), DATEADD(day, @TimeRange, EndTime)
    from TimeRanges
    where EndTime < @Finish )
  select StartTime, EndTime, Count( Test.ScenarioID ) as TotalPeaks
    from TimeRanges as TR left outer join
      dbo.Test as Test on TR.StartTime <= Test.SessionStartTime and Test.SessionCloseTime < TR.EndTime
    group by TR.StartTime, TR.EndTime   
END
于 2012-10-22T08:33:35.997 回答