SQL 将星期四作为星期计数器查找一个月的星期数的服务器查询

SQL Server query for finding weeknumber of a month considering thursday as week counter

我需要一个月中的周数。一周需要从周四开始。

我已经尝试了很多答案,但我无法满足以下条件。

例如:以下日期的周数是

SL  Date        Weeknumber   
---------------------------
1) 21/09/2017 - 3
2) 28/09/2017 - 4
3) 23/03/2017 - 4
4) 29/06/2017 - 5
5) 15/03/2018 - 3
6) 28/03/2018 - 4
7) 29/03/2018 - 5

提前致谢

假设您的输入 table 类似于

TableOfDates([SL] int,[Date] date)

以下查询将给出正确的周数

set datefirst 4
go

--create table TableOfDates([SL] int,[Date] datetime)
--insert into TableOfDates values 
--(1,'09-21-2017'),
--(2,'09-28-2017'),
--(3,'03-23-2017')

select 

    t.Date,DATEPART(wk,t.[Date])- DATEPART(wk,DATEADD(d,1,DATEADD(m,-1,EOMONTH(t.[Date]))))
from TableOfDates t

go
--drop table TableOfDates
set datefirst 7
go

对于SQL SERVER 2008版本

您可以使用以下查询

select 

    t.Date,DATEPART(wk,t.[Date])- DATEPART(wk,cast(dateadd(m,datediff(m,0,t.date),0) as date))
from TableOfDates t

说明

Set Datefirst 默认设置为 7。我们指示 SQL 服务器使用星期四 (4) 作为一周的开始。 请参阅 MSDN documentation

现在由于周数是从年初开始计算的,我们需要每月的周数,我们计算当前日期和月初的周数,它们的差值给出每月的周数

最后我们将 datefirst 设置回默认值 7

见附件输出

使用DATAFIRST.

来自 MSDN:-

Sets the first day of the week to a number from 1 through 7.

╔═══════════════════════════╦══════════════════════════╗
║           Value           ║ First day of the week is ║
╠═══════════════════════════╬══════════════════════════╣
║ 1                         ║ Monday                   ║
║ 2                         ║ Tuesday                  ║
║ 3                         ║ Wednesday                ║
║ 4                         ║ Thursday                 ║
║ 5                         ║ Friday                   ║
║ 6                         ║ Saturday                 ║
║ 7 (default, U.S. English) ║ Sunday                   ║
╚═══════════════════════════╩══════════════════════════╝

所以使用下一个代码:-

Set DATEFIRST 4
DECLARE @DATE DATETIME
SET @DATE = '2017-09-21'

SELECT DATEPART(WEEK, @DATE)  -
    DATEPART(WEEK, DATEADD(MM, DATEDIFF(MM,0,@DATE), 0)) AS WEEK_OF_MONTH

结果:-

3

这应该能满足您的需求。有点疯狂,我知道,但它有效:

create table #months (date date);

insert into #months
values ('20170921')
    , ('20170928')
    , ('20170323')
    , ('20170629')
    , ('20180315')
    , ('20180328')
    , ('20180329');

select date
    , case 
        when weeksdif * 7 <= daysdiff
            then weeksdif + 1
        else weeksdif
        end [week nr]
from (
    select *
        , wk - wk_startOfMonth [weeksdif]
        , DATEDIFF(DAY, startOfMonth, date) [daysdiff]
    from (
        select *
            , DATEPART(wk, startOfMonth) wk_startOfMonth
            , DATEPART(WEEKDAY, startOfMonth) wkd_startOfMonth
        from (
            select *
                , DATEPART(wk, date) wk
                , DATEPART(weekday, date) wkd
                , cast( (CAST(YEAR(date) as varchar(8)) 
                        + RIGHT('0' + CAST(month(date) as varchar(8)), 2) 
                        + '01') as date) startOfMonth
            from #months
            ) result_set
        ) result_set
    ) result_set
order by date;