获取正确的12月31日星期几

时间:2019-01-02 14:05:00

标签: sql-server tsql

在SQL Server中,我试图获取给定日期(欧洲风格)的星期数(含年份),因此我将DATEPARTISO_WEEK参数一起使用:

SELECT CAST(DATEPART(year, myDate) AS VARCHAR) + RIGHT('0' + CAST(DATEPART(ISO_WEEK, myDate) AS VARCHAR), 2);

这很好用,除了2018年12月31日(在2019年的第一周),但是由于我分别在年份和一周中使用DATEPART,所以这显然行不通。这是myDate为31-12-2018的示例:

SELECT CAST(DATEPART(year, '31-12-2018') AS VARCHAR) + RIGHT('0' + CAST(DATEPART(ISO_WEEK, '31-12-2018') AS VARCHAR), 2);

以上查询返回“ 201801”。

是否有一种方法可以简单地获取2018年12月31日的201901,而无需对此日期进行明确测试?

3 个答案:

答案 0 :(得分:2)

查看可能重复的参考文​​献并整理一下:

declare @d datetime = '2018-12-31';
select cast(year(dateadd(day, 26 - datepart(ISO_WEEK, @d), @d)) as varchar(4)) + right(('0' + cast(datepart(ISO_WEEK, @d) as varchar(2))), 2)

得出201901

测试代码:

select cast(ds.d as date) as 'date', cast(year(dateadd(day, 26 - datepart(ISO_WEEK, ds.d), ds.d)) as varchar(4)) + right(('0' + cast(datepart(ISO_WEEK, ds.d) as varchar(2))), 2) as "IsoWeek"
from (
    select dateadd(day, x.num, '2018-12-30') as d
    from (
        select h.num + d.num + s.num as num
        from (
            select 0 num union select 100 num union select 200 num union select 300 num
        ) h
        cross join (
            select 0 num union select 10 num union select 20 num union select 30 num
        ) d
        cross join (
            select 0 num union select 1 num union select 2 union select 3 num
        ) s
    ) x    
) ds
order by ds.d

结果为:

date        IsoWeek
2018-12-30  201852
2018-12-31  201901
2019-01-01  201901
2019-01-02  201901
2019-01-09  201902
2019-01-10  201902
2019-01-11  201902
2019-01-12  201902
2019-01-19  201903
2019-01-20  201903
2019-01-21  201904
2019-01-22  201904
2019-01-29  201905
2019-01-30  201905
2019-01-31  201905
2019-02-01  201905
2019-04-09  201915
2019-04-10  201915
2019-04-11  201915
2019-04-12  201915
2019-04-19  201916
2019-04-20  201916
2019-04-21  201916
2019-04-22  201917
2019-04-29  201918
2019-04-30  201918
2019-05-01  201918
2019-05-02  201918
2019-05-09  201919
2019-05-10  201919
2019-05-11  201919
2019-05-12  201919
2019-07-18  201929
2019-07-19  201929
2019-07-20  201929
2019-07-21  201929
2019-07-28  201930
2019-07-29  201931
2019-07-30  201931
2019-07-31  201931
2019-08-07  201932
2019-08-08  201932
2019-08-09  201932
2019-08-10  201932
2019-08-17  201933
2019-08-18  201933
2019-08-19  201934
2019-08-20  201934
2019-10-26  201943
2019-10-27  201943
2019-10-28  201944
2019-10-29  201944
2019-11-05  201945
2019-11-06  201945
2019-11-07  201945
2019-11-08  201945
2019-11-15  201946
2019-11-16  201946
2019-11-17  201946
2019-11-18  201947
2019-11-25  201948
2019-11-26  201948
2019-11-27  201948
2019-11-28  201948

答案 1 :(得分:1)

问题在于,在特殊情况下,星期几在一年中可能非常“早”,尽管实际上是第12个月,因此很容易检查12月是否有年份的开始,在这种情况下,只需在年份中加一个,

DECLARE @mydate as datetime2 = '20270101';
SELECT CAST(DATEPART(year, @myDate) 
            + case when DATEPART(ISO_WEEK, @myDate) < 2 and month(@mydate) =12 then 
                                                      1 
                                                 else 
                                                       0 
                                                 end   

             + case when DATEPART(ISO_WEEK, @myDate) >10 and month(@mydate) =1 then 
                                                  -1 
                                             else 
                                                   0 
                                             end 
AS VARCHAR) 
              + RIGHT('0' + CAST(DATEPART(ISO_WEEK, @myDate) AS VARCHAR), 2);

我对另一个答案中突出显示的2027问题进行了更正。

答案 2 :(得分:0)

检查以下查询,使用星期几来获取年份值

SELECT CAST(DATEPART(year, DATEADD(wk, 1, DATEADD(DAY, 0-DATEPART(WEEKDAY, '2018-12-31'), DATEDIFF(dd, 0, '2018-12-31')))) AS VARCHAR) + RIGHT('0' + CAST(DATEPART(ISO_WEEK, '2018-12-31') AS VARCHAR), 2);