11

如果我有 2 个日期,我知道我可以使用 计算出两个日期之间的天数、小时数、分钟数等datediff,例如:

declare @start datetime;
set @start = '2013-06-14';

declare @end datetime;
set @end = '2013-06-15';

select datediff( hour, @start, @end );

如何确定日期范围是否包括周末?

我想知道日期范围是否包括周末的原因是因为我想从天数或小时数中减去周末。即如果开始日期是星期五,结束日期是星期一,我应该只有 1 天或 24 小时。

Datepart 1 = 星期日,datepart 7 = 我服务器上的星期六。

4

7 回答 7

8

我有一个计算两个日期之间工作日的函数,基本查询是

declare @start datetime;
set @start = '2013-06-14';

declare @end datetime;
set @end = '2013-06-17';
SELECT 
   (DATEDIFF(dd, @Start, @end) +1)  -- total number of days (inclusive)
  -(DATEDIFF(wk, @Start, @end) * 2) -- number of complete weekends in period
  -- remove partial weekend days, ie if starts on sunday or ends on saturday
  -(CASE WHEN DATENAME(dw, @Start) = 'Sunday' THEN 1 ELSE 0 END) 
  -(CASE WHEN DATENAME(dw, @end) = 'Saturday' THEN 1 ELSE 0 END) 

因此,如果工作日与 datediff 的天数不同,您可以计算出日期是否包括周末

  SELECT case when  (DATEDIFF(dd, @Start, @end) +1) <>
   (DATEDIFF(dd, @Start, @end) +1)  -- total number of days (inclusive)
  -(DATEDIFF(wk, @Start, @end) * 2) -- number of complete weekends in period
  -- remove partial weekend days, ie if starts on sunday or ends on saturday
  -(CASE WHEN DATENAME(dw, @Start) = 'Sunday' THEN 1 ELSE 0 END) 
  -(CASE WHEN DATENAME(dw, @end) = 'Saturday' THEN 1 ELSE 0 END) then 'Yes' else 'No' end as IncludesWeekends

或更简单

SELECT   (DATEDIFF(wk, @Start, @end) * 2) +(CASE WHEN DATENAME(dw, @Start) = 'Sunday' THEN 1 ELSE 0 END)      +(CASE WHEN DATENAME(dw, @end) = 'Saturday' THEN 1 ELSE 0 END)  as weekendDays
于 2013-06-14T14:51:11.253 回答
3

You have a weekend day if any one of the following three conditions is true:

  1. The day of week (as an integer) of the end date is less than the day of week of the start date

  2. Either day is itself a weekend day

  3. The range includes at least six days

.

select 
    Coalesce(
    --rule 1
    case when datepart(dw,@end) - datepart(dw,@start) < 0 then 'Weekend' else null end,
    -- rule 2
    -- depends on server rules for when the week starts
    -- I think this code uses sql server defaults
    case when datepart(dw,@end) in (1,7) or datepart(dw,@start) in (1,7) then 'Weekend' else null end,
    --rule 3
    -- six days is long enough
    case when datediff(d, @start, @end) >= 6 then 'Weekend' Else null end,
    -- default
    'Weekday')
于 2013-06-14T14:49:49.037 回答
1

一种方法,只是向您展示如何为此使用数字表

declare @start datetime;
set @start = '2013-06-14';

declare @end datetime;
set @end = '2013-06-15'; -- play around by making this 2013-06-14 and other dates


IF EXISTS (SELECT * FROM(
SELECT DATEADD(dd,number,@start) AS SomeDAte 
FROM master..spt_values
WHERE type = 'P'
AND DATEADD(dd,number,@start) BETWEEN @start AND @end) x
WHERE DATEPART(dw,SomeDate) IN(1,7))  -- US assumed here
SELECT 'Yes'
ELSE
SELECT 'No'

返回两个日期之间的所有周末的示例

declare @start datetime;
set @start = '2013-06-14';

declare @end datetime;
set @end = '2013-06-30';



SELECT DATEADD(dd,number,@start) AS SomeDAte 
FROM master..spt_values
WHERE type = 'P'
AND DATEADD(dd,number,@start) BETWEEN @start AND @end
AND DATEPART(dw,DATEADD(dd,number,@start)) IN(1,7)

结果

2013-06-15 00:00:00.000
2013-06-16 00:00:00.000
2013-06-22 00:00:00.000
2013-06-23 00:00:00.000
2013-06-29 00:00:00.000
2013-06-30 00:00:00.000
于 2013-06-14T14:51:55.380 回答
0

您可以使用递归 CTE 来获取范围之间的日期

     WITH    CTE_DatesTable
          AS ( SELECT   @MinDate AS [EffectiveDate]
               UNION ALL
               SELECT   DATEADD(dd, 1, [EffectiveDate])
               FROM     CTE_DatesTable
               WHERE    DATEADD(dd, 1, [EffectiveDate]) <= @MaxDate )

            SELECT  [EffectiveDate]
            FROM    CTE_DatesTable
    OPTION  ( MAXRECURSION 0 );

然后使用 .. 过滤掉周末

((DATEPART(dw, DT.EffectiveDate) + @@DATEFIRST) % 7) NOT IN (0, 1) 
于 2013-06-14T15:34:36.767 回答
0

您可以使用以下功能。如果它从周末开始,第一个将给定的开始日期或结束日期移动到星期一(如果向后,则为星期五)。第二个计算没有周末的两个日期之间的秒数。然后你只需要检查总天数是否等于没有 weksends 的天数(下面的演示)。

CREATE FUNCTION [dbo].[__CorrectDate](
    @date DATETIME,
    @forward INT
)

RETURNS DATETIME AS BEGIN
    IF (DATEPART(dw, @date) > 5) BEGIN

        IF (@forward = 1) BEGIN
            SET @date = @date + (8 - DATEPART(dw, @date))
            SET @date = DateAdd(Hour, (8 - DatePart(Hour, @date)), @date)
        END ELSE BEGIN
            SET @date = @date - (DATEPART(dw, @date)- 5)
            SET @date = DateAdd(Hour, (18 - DatePart(Hour, @date)), @date)
        END
        SET @date = DateAdd(Minute, -DatePart(Minute, @date), @date)
        SET @date = DateAdd(Second, -DatePart(Second, @date), @date)
    END

    RETURN @date
END

GO

CREATE FUNCTION [dbo].[__DateDiff_NoWeekends](
    @date1 DATETIME,
    @date2 DATETIME
)

RETURNS INT AS BEGIN
    DECLARE @retValue INT

    SET @date1 = dbo.__CorrectDate(@date1, 1)
    SET @date2 = dbo.__CorrectDate(@date2, 0)

    IF (@date1 >= @date2)
        SET @retValue = 0
    ELSE BEGIN
        DECLARE @days INT, @weekday INT
        SET @days = DATEDIFF(d, @date1, @date2)
        SET @weekday = DATEPART(dw, @date1) - 1

        SET @retValue = DATEDIFF(s, @date1, @date2) - 2 * 24 * 3600 * ((@days + @weekday) / 7) 
    END

    RETURN @retValue
END

然后你可以通过这种方式获取信息:

declare @start datetime
set @start = '20130614'

declare @end datetime
set @end = '20130615'

declare @daysTotal int
declare @daysWoWeekends int

SET @daysTotal = DATEDIFF(dd, @start, @end)
SET @daysWoWeekends = dbo.__DateDiff_NoWeekends(@start, @end) / (24 * 3600)

SELECT CASE WHEN @daysTotal = @daysWoWeekends
       THEN 'No weekend between'
       ELSE 'There are weeksends' END,
       @daysTotal,
       @daysWoWeekends,@start,@end

这是一个演示http ://sqlfiddle.com/#!6/7cda7/11

There are weeksends 1   0   June, 14 2013 00:00:00+0000 June, 15 2013 00:00:00+0000
于 2013-06-14T15:08:27.187 回答
0

这是简单而通用的查询。您可以通过递归查询获得结果。检查以下查询

with mycte as
(
  select cast('2013-06-14' as datetime) DateValue
  union all
  select DateValue + 1 from mycte where DateValue + 1 < '2013-06-17'
)

select count(*) as days , count(*)*24 as hours
from    mycte
WHERE DATENAME(weekday ,DateValue) != 'SATURDAY' AND 
DATENAME(weekday ,DateValue) != 'SUNDAY'
OPTION (MAXRECURSION 0)

它肯定会为你工作。

于 2013-06-14T15:16:23.033 回答
0

类似的问题,尽管在我的情况下,我想要一个使用 Impala SQL 的两个日期是否包含该 DOW 的 onehot 编码的二进制列列表(周一至周日)。

select ...

max(case when tripduration > 6 then 1
         when 2 >= dayofweek(tripstartdate) and 2 <= dayofweek(tripstartdate) + tripduration then 1
         when 2 <= dayofweek(tripenddate)   and 2 >= dayofweek(tripenddate) - tripduration   then 1 else 0 end) as '2 mon',

max(case when tripduration > 6 then 1
         when 3 >= dayofweek(tripstartdate) and 3 <= dayofweek(tripstartdate) + tripduration then 1
         when 3 <= dayofweek(tripenddate)   and 3 >= dayofweek(tripenddate) - tripduration   then 1 else 0 end) as '3 tue',

max(case when tripduration > 6 then 1
         when 4 >= dayofweek(tripstartdate) and 4 <= dayofweek(tripstartdate) + tripduration then 1
         when 4 <= dayofweek(tripenddate)   and 4 >= dayofweek(tripenddate) - tripduration   then 1 else 0 end) as '4 wed',

max(case when tripduration > 6 then 1
         when 5 >= dayofweek(tripstartdate) and 5 <= dayofweek(tripstartdate) + tripduration then 1
         when 5 <= dayofweek(tripenddate)   and 5 >= dayofweek(tripenddate) - tripduration   then 1 else 0 end) as '5 thu',

max(case when tripduration > 6 then 1
         when 6 >= dayofweek(tripstartdate) and 6 <= dayofweek(tripstartdate) + tripduration then 1
         when 6 <= dayofweek(tripenddate)   and 6 >= dayofweek(tripenddate) - tripduration   then 1 else 0 end) as '6 fri',

max(case when tripduration > 6 then 1
         when 7 >= dayofweek(tripstartdate) and 7 <= dayofweek(tripstartdate) + tripduration then 1
         when 7 <= dayofweek(tripenddate)   and 7 >= dayofweek(tripenddate) - tripduration   then 1 else 0 end) as '7 sat',

max(case when tripduration > 6 then 1
         when 1 >= dayofweek(tripstartdate) and 1 <= dayofweek(tripstartdate) + tripduration then 1
         when 1 <= dayofweek(tripenddate)   and 1 >= dayofweek(tripenddate) - tripduration   then 1 else 0 end) as '1 sun'

from ....
于 2020-06-20T11:26:49.470 回答