Get first Sunday of next month using T-SQL

后端 未结 9 2111
梦毁少年i
梦毁少年i 2021-02-09 13:19

Looking for a way to get the date in the format \"11/1/2009\", which would be the first sunday of next month. I want to run this query after the first sunday in october to get

相关标签:
9条回答
  • 2021-02-09 13:45

    Reference taken from this blog:

    SQL Server 2012 introduced one new TSQL EOMONTH to return the last day of the month that contains the specified date with an optional offset.

    CREATE TABLE tbl_Test_EOMONTH
    (
        SampleDate DATETIME
    )
    GO
    
    INSERT INTO tbl_Test_EOMONTH VALUES ('2015-12-20')
    INSERT INTO tbl_Test_EOMONTH VALUES ('2015-11-08')
    INSERT INTO tbl_Test_EOMONTH VALUES ('2015-10-16')
    INSERT INTO tbl_Test_EOMONTH VALUES ('2015-09-26')
    INSERT INTO tbl_Test_EOMONTH VALUES ('2016-01-31') 
    GO
    
    SELECT 
        DATEADD(DAY,8-DATEPART(WEEKDAY,DATEADD(DAY,0,EOMONTH([SampleDate])))
        ,EOMONTH([SampleDate])) AS FirstSunday_ofTheNextMonth
    FROM tbl_Test_EOMONTH
    GO
    
    0 讨论(0)
  • 2021-02-09 13:50

    Just an FYI rather then coming up with some code to do this how about using a calendar table.

    Take a look at this: http://web.archive.org/web/20070611150639/http://sqlserver2000.databases.aspfaq.com/why-should-i-consider-using-an-auxiliary-calendar-table.html

    This also may help: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=99696

    0 讨论(0)
  • 2021-02-09 13:54

    Here is the non-system specific way to determine the first Sunday of the following month:

    First, get the current month and add one month. Next, set the date of that variable to be on the first. Next, find the day value of that date (let's assume Mondays are 1 and Sundays are 7). Next, subtract the day value of the 1st of the month from the day value of Sunday (7).

    You now have the number of days between the first of the month and the first Sunday. You could then add that to the date variable to get the first Sunday, or, since we know the first of the month is 1, you could just add one to the difference (found in that last step above) and that is the date of the first Sunday. (You have to add one because it's subtracting and thus if the first of the given month IS Sunday, you'd end up with 0).

    I have been looking through the T-SQL documentation and it is not at all intuitive as to how how you would use my method, but you will need the concept of "day of week number" to make it work no matter what.

    0 讨论(0)
  • 2021-02-09 13:58

    You can use DATENAME to determine the day you want, I might recommend a loop to move the date from the 01 of the month in question to get to the first sunday.

    So lets try:

    DECLARE @DateTime DATETIME
    

    Set to the date to start off with, then add 1 day until you find what you are looking for. Use datename with dw...

    We have used this to determine weekends, but holidays will be a problem, where we use a table to store that.

    0 讨论(0)
  • 2021-02-09 14:00

    Try this code as a function:

    -- Variables
    DECLARE @DATE DATETIME
    DECLARE @DAY INT
    DECLARE @DAYOFWEEK INT
    DECLARE @TESTDATE DATETIME
    
    -- Set
    SET @TESTDATE = GETDATE()
    SET @DATE = DATEADD( MONTH, 1, @TESTDATE )
    SET @DAY = DATEPART( DAY, @TESTDATE )
    SET @DATE = DATEADD( DAY, -@DAY + 1, @DATE )
    SET @DAYOFWEEK = DATEPART( WEEKDAY, @DATE )
    IF @DAYOFWEEK > 1
    BEGIN
        SET @DAYOFWEEK = 8 - @DAYOFWEEK
    END
    ELSE
    BEGIN
        SET @DAYOFWEEK = 0
    END
    
    SET @DATE = DATEADD( DAY, @DAYOFWEEK, @DATE )
    
    -- Display
    PRINT @TESTDATE
    PRINT @DAY
    PRINT @DAYOFWEEK
    PRINT @DATE
    
    0 讨论(0)
  • 2021-02-09 14:03

    try this:

    Declare @D Datetime 
    Set @D = [Some date for which you want the following months' first sunday]
    Select DateAdd(day, (8-DatePart(weekday, 
        DateAdd(Month, 1+DateDiff(Month, 0, @D), 0)))%7, 
        DateAdd(Month, 1+DateDiff(Month, 0, @D), 0))
    

    EDIT Notes:

    The first of next Month is given by the expression:

    DateAdd(Month, 1+DateDiff(Month, 0, @D), 0)
    

    or by: which can be modified to give the first of the month two months from now by changing the 1 to a 2:

    DateAdd(Month, 2+DateDiff(Month, 0, @D), 0) 
    

    EDIT: In response to @NissanFan, and @Anthony: to modify this to return the first Monday Tuesday Wednesday, etc, change the value 8 to a 9, 10, 11, etc....

    Declare @Sun TinyInt Set @Sun = 8
    Declare @Mon TinyInt Set @Mon = 9
    Declare @Tue TinyInt Set @Tue = 10
    Declare @Wed TinyInt Set @Wed = 11
    Declare @Thu TinyInt Set @Thu = 12
    Declare @Fri TinyInt Set @Fri = 13
    Declare @Sat TinyInt Set @Sat = 14
    Declare @D Datetime, @FONM DateTime -- FirstofNextMonth 
    Set @D = [Some date for which you want the following months' first sunday]
    Set @FONM = DateAdd(Month, 1+DateDiff(Month, 0, @D),0)
    
    Select 
      DateAdd(day, (@Sun -DatePart(weekday, @FONM))%7, @FONM) firstSunInNextMonth,
      DateAdd(day, (@Mon -DatePart(weekday, @FONM))%7, @FONM) firstMonInNextMonth,
      DateAdd(day, (@Tue -DatePart(weekday, @FONM))%7, @FONM) firstTueInNextMonth,
      DateAdd(day, (@Wed -DatePart(weekday, @FONM))%7, @FONM) firstWedInNextMonth,
      DateAdd(day, (@Thu -DatePart(weekday, @FONM))%7, @FONM) firstThuInNextMonth,
      DateAdd(day, (@Fri -DatePart(weekday, @FONM))%7, @FONM) firstFriInNextMonth,
      DateAdd(day, (@Sat -DatePart(weekday, @FONM))%7, @FONM) firstSatInNextMonth
    
    0 讨论(0)
提交回复
热议问题