sql server 2005 -获取两个日期之间的工作日数(星期日,星期一,星期二)



所以,我想,对于开始日期和结束日期,确定在这两个日期之间一周中有多少特殊的日子。

那么有多少个星期一、星期二等等

我知道我可以在开始日期和结束日期之间循环并每天检查,但差异可能会有很多天。我更喜欢不需要循环的东西。什么好主意吗?(SQL Server 2005+必须支持)

考虑到我认为你想要得到的东西,这个应该可以做到:

SET DATEFIRST 1
DECLARE
    @start_date DATETIME,
    @end_date DATETIME
SET @start_date = '2011-07-11'
SET @end_date = '2011-07-22'
;WITH Days_Of_The_Week AS (
    SELECT 1 AS day_number, 'Monday' AS day_name UNION ALL
    SELECT 2 AS day_number, 'Tuesday' AS day_name UNION ALL
    SELECT 3 AS day_number, 'Wednesday' AS day_name UNION ALL
    SELECT 4 AS day_number, 'Thursday' AS day_name UNION ALL
    SELECT 5 AS day_number, 'Friday' AS day_name UNION ALL
    SELECT 6 AS day_number, 'Saturday' AS day_name UNION ALL
    SELECT 7 AS day_number, 'Sunday' AS day_name
)
SELECT
    day_name,
    1 + DATEDIFF(wk, @start_date, @end_date) -
        CASE WHEN DATEPART(weekday, @start_date) > day_number THEN 1 ELSE 0 END -
        CASE WHEN DATEPART(weekday, @end_date)   < day_number THEN 1 ELSE 0 END
FROM
    Days_Of_The_Week

我不确定OP之后是什么,这将给出每周每天的计数:

SET DATEFIRST 1
DECLARE @StartDate datetime
       ,@EndDate datetime
SELECT @StartDate='7/13/2011'
      ,@EndDate='7/28/2011'
;with AllDates AS
(
    SELECT @StartDate AS DateOf, datename(weekday,@StartDate) AS WeekDayName, datepart(weekday,@StartDate) AS WeekDayNumber
    UNION ALL
    SELECT DateOf+1, datename(weekday,DateOf+1), datepart(weekday,DateOf+1)
        FROM AllDates
    WHERE DateOf<@EndDate
)
SELECT COUNT(*) CountOf,WeekDayName  FROM AllDates GROUP BY WeekDayName,WeekDayNumber ORDER BY WeekDayNumber
输出:

CountOf     WeekDayName
----------- ------------------------------
2           Monday
2           Tuesday
3           Wednesday
3           Thursday
2           Friday
2           Saturday
2           Sunday
(7 row(s) affected)

这将给出星期一到星期五的天数:

SET DATEFIRST 1
DECLARE @StartDate datetime
       ,@EndDate datetime
SELECT @StartDate='7/13/2011'
      ,@EndDate='7/28/2011'
;with AllDates AS
(
    SELECT @StartDate AS DateOf, datepart(weekday,@StartDate) AS WeekDayNumber
    UNION ALL
    SELECT DateOf+1, datepart(weekday,DateOf+1)
        FROM AllDates
    WHERE DateOf<@EndDate
)
SELECT COUNT(*) AS WeekDayCount FROM AllDates WHERE WeekDayNumber<=5
输出:

WeekDayCount
------------
12
(1 row(s) affected)

如果您有一个假日表,您可以加入它并删除那些。这里有一个稍微不同的版本,可能执行得更好:

SET DATEFIRST 1
DECLARE @StartDate datetime
       ,@EndDate datetime
SELECT @StartDate='7/13/2011'
      ,@EndDate='7/28/2011'
;with AllDates AS
(
    SELECT @StartDate AS DateOf, datepart(weekday,getdate()) AS WeekDayNumber
    UNION ALL
    SELECT DateOf+1, (WeekDayNumber+1) % 7
        FROM AllDates
    WHERE DateOf<@EndDate
)
SELECT COUNT(*) AS WeekDayCount FROM AllDates WHERE WeekDayNumber>0 AND WeekDayNumber<6
--I don't like using "BETWEEN", ">", ">=", "<", and "<=" are more explicit in defining end points

产生与原始查询相同的输出。

这是标准设置,但可以调整

DECLARE @StartDate datetime, @EndDate datetime
SELECT @StartDate='20110601', @EndDate='20110630'
;WITH AllDates AS
(
    SELECT @StartDate AS DateOf, datepart(weekday, @StartDate) AS WeekDayNumber
    UNION ALL
    SELECT DateOf+1, datepart(weekday, DateOf+1) 
    FROM AllDates
    WHERE DateOf < @EndDate
)
SELECT SUM(CASE WHEN WeekDayNumber BETWEEN 2 AND 6 THEN 1 ELSE 0 END) AS WeekDayCount
FROM AllDates
OPTION (MAXRECURSION 0)

这应该对SQL Server有效,并且应该是国际化安全的(注意:我做没有有一个服务器来测试它)。

SELECT datediff(day, @start, @end) - datediff(week, @start, @end) * 2 
                                   - CASE WHEN datepart(weekday, @start) 
                                               IN (datepart(weekday, '1970-01-03'), 
                                                   datepart(weekday, '1970-01-04')) 
                                          THEN 1
                                          ELSE 0 END, 
                                   - CASE WHEN datepart(weekday, @end) 
                                               IN (datepart(weekday, '1970-01-03'), 
                                                   datepart(weekday, '1970-01-04')) 
                                          THEN 1
                                          ELSE 0 END

试一试。


给出了说明,这应该得到每天的天数。
不使用递归,并且应该是完全国际安全的。您必须根据需要调整包含/排除的开始/结束日期参数(例如,我用来检查这一点的DB2版本排除了开始日期,但包括了结束日期)。

WITH dayOfWeek (name, dayNumber) as (VALUES(dayname(weekday, '1970-01-01'), daypart(weekday, '1970-01-01')), 
                                           (dayname(weekday, '1970-01-02'), daypart(weekday, '1970-01-02')), 
                                           (dayname(weekday, '1970-01-03'), daypart(weekday, '1970-01-03')), 
                                           (dayname(weekday, '1970-01-04'), daypart(weekday, '1970-01-04')), 
                                           (dayname(weekday, '1970-01-05'), daypart(weekday, '1970-01-05')), 
                                           (dayname(weekday, '1970-01-06'), daypart(weekday, '1970-01-06')), 
                                           (dayname(weekday, '1970-01-07'), daypart(weekday, '1970-01-07')))
SELECT name, dayNumber, datediff(weeks, @start, @end)
                        + CASE WHEN datepart(weekday, @end) >= dayNumber THEN 1 ELSE 0 END
                        - CASE WHEN datepart(weekday, @start) >= dayNumber THEN 1 ELSE 0 END
FROM dayOfWeek

有帮助吗?

您可以使用DATEDIFFDATEPART函数+一些基本的数学来获得所需的结果,而无需循环。

我本可以简单地将其作为评论添加到标记的答案中,但没有足够的"声誉"。不需要硬编码依赖于datefirst的day_number,您可以通过发现一周中的每一天的工作日来设置它:

;WITH Days_Of_The_Week AS (
        SELECT DATEPART(dw, '2007-01-01') AS day_number, 'Monday' AS day_name UNION ALL -- 2007-01-01 is a known Monday
        SELECT DATEPART(dw, '2007-01-02') AS day_number, 'Tuesday' AS day_name UNION ALL
        SELECT DATEPART(dw, '2007-01-03') AS day_number, 'Wednesday' AS day_name UNION ALL
        SELECT DATEPART(dw, '2007-01-04') AS day_number, 'Thursday' AS day_name UNION ALL
        SELECT DATEPART(dw, '2007-01-05') AS day_number, 'Friday' AS day_name UNION ALL
        SELECT DATEPART(dw, '2007-01-06') AS day_number, 'Saturday' AS day_name UNION ALL
        SELECT DATEPART(dw, '2007-01-07') AS day_number, 'Sunday' AS day_name
    )
@start_date date = '2017-08-11',    
@end_date date = '2017-08-27',    
@weekday int = 7,    
@count int output    
As    
Begin    
Declare @i int = 0
set @count = 0
while(@i <= (select Datediff(Day, @start_date, @end_date)))
begin
    if(Dateadd(Day, @i, @start_date) > @end_date)
        break
    if(Datepart(weekday, Dateadd(Day, @i, @start_date)) = @weekday)
        set @count += 1
    set @i += 1
end
select @count

相关内容

  • 没有找到相关文章

最新更新