获取每月第二个星期二的日期

时间:2013-03-22 07:38:36

标签: sql tsql

有没有办法使用tsql语法找出每月第二个星期二的日期? 例如。 3月份是12月,4月份是9号。

Thx&干杯 亚历

4 个答案:

答案 0 :(得分:3)

这就是你如何在2013年找到所有“第二个星期二”。

select 
dateadd(day, 8, datediff(day, 1, dateadd(month, n, '2013-01-07')) / 7 * 7) date
from 
(values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11)) t(n)

答案 1 :(得分:2)

在不知道实际需要的输入和输出是什么的情况下,我现在可以给你的是一个谓词,用于将日期标识为其当月的第二个星期二:

DATEPART(day,@Date) between 8 and 14 and --Find the second one in the month
DATEPART(weekday,@Date) = DATEPART(weekday,'20130319') --Make sure its a Tuesday

(我使用固定的,已知的星期二,以避免在运行查询时必须知道DATEFIRST设置有效)


这会找到当月的相应星期二,但显然可以将@Date设置为任何感兴趣的日期:

declare @Date datetime
set @Date =  CURRENT_TIMESTAMP

;with Numbers as (select n from (values (0),(1),(2),(3),(4),(5),(6)) t(n)),
PotentialDates as (select DATEADD(day,n,DATEADD(month,DATEDIFF(month,'20010101',@Date),'20010108')) as Date
from Numbers
)
select * from PotentialDates where DATEPART(weekday,Date) = DATEPART(weekday,'20130319')

(并且,显然,希望查询可以是更大查询的一部分,其中@Date是列值,因此这可以构成整个工作的基于集合的方法的一部分)

答案 2 :(得分:1)

此代码将为您提供每月的第1和第3个星期日..所以用SQL欢呼: - )

declare @dt datetime
select @dt = '12/01/2014'

select dateadd(mm,datediff(mm,'',@dt),'') - datepart(dw,dateadd(mm,datediff(mm,'',@dt),'')+0)+ 8
select dateadd(mm,datediff(mm,'',@dt),'') - datepart(dw,dateadd(mm,datediff(mm,'',@dt),'')+0)+ 22

答案 3 :(得分:1)

从星期日开始的几个月内,以前的答案不起作用(而是指向第二个星期日)。

SELECT @dt AS input_date,

    DATEADD(mm, DATEDIFF(mm, 0, @dt), 0) --truncate date to month start

    -- DATEPART(@month_start) returns month start's weekday, with Sunday starting 1;
    -- Since Sunday starts at 1, we need to perform proper adjustment - move date 6 days forward (7 week days - 1 for sunday) forward and find its datepart, which will be 7
    -- Result: month starting sunday, datepart returns 7; month starting Mon we return 1 (datepart of Mon + 6 days = Sunday, which is 1), month starting tue, we return 2
    -- Effectivelly, datepart offset will always point last Sunday of previous month
    - DATEPART(dw, 
            6
            + DATEADD(mm,datediff(mm,0,@dt),0) --truncate date to month start
        ) 
        
    -- Since we found last Sunday of previous month, we need to add 7
    + 7 AS CORRECT,

    dateadd(mm,datediff(mm,'',@dt),'') - datepart(dw,dateadd(mm,datediff(mm,'',@dt),'')+0)+ 8 AS sometimes_correct

显示相对于 Pravin Pandit 答案的正确答案的图片:

1

我们可以扩展查找当月第一个周二的基本原理并创建一个函数来执行此操作,因此对于任何输入日期,它将查找相关月份的第一个周二:

ALTER FUNCTION dbo.f_time_floor_1st_tue(@date DATETIME2(3))
RETURNS DATETIME
AS
BEGIN
    RETURN 
    
            DATEADD(mm, DATEDIFF(mm, 0, @date), 0) --truncate date to month start

        -- DATEPART(@month_start) returns month start's weekday, with Sunday starting 1;
        -- Since Sunday starts at 1, we need to perform proper adjustment - move date 6 days forward (7 week days - 1 for sunday) forward and find its datepart, which will be 7
        -- Result: month starting sunday, datepart returns 7; month starting Mon we return 1 (datepart of Mon + 6 days = Sunday, which is 1), month starting tue, we return 2
        -- Effectivelly, datepart offset will always point last Sunday of previous month
        -- Extending this logic for finding first Tuesday, Tuesday should always return 7 we need to move Tue datepart (3) by 4  ( which is 7 days in the week minus 3 
        - DATEPART(dw, 
                4 -- 4 is adjustment so that DATEPART returns 7 for all months starting Tue
                + DATEADD(mm,datediff(mm,0,@date),0) --truncate date to month start
            ) 
        
        -- Since we found last weekday of previous month, we need to add 7
        + 7
    
    ;
END;
GO