How to find the Nearest (day of the week) for a given date

后端 未结 3 617
长情又很酷
长情又很酷 2021-01-22 18:32

I been practicing queries, and my current scenario is to find the nearest Saturday for a given date. After i got the logic down, i came up with a, whats looks like a long and me

相关标签:
3条回答
  • 2021-01-22 19:01

    Well not exactly an answer, but an alternative

        DECLARE @DATE DATE,
        @SUN DATE,
        @MON DATE,
        @TUE DATE,
        @WED DATE,
        @THU DATE,
        @FRI DATE,
        @SAT DATE
    
               SET @DATE = '2013-09-14'
    
        SET @SUN = DATEADD(DAY,-1,@DATE)
        SET @MON = DATEADD(DAY,-1,@DATE)
        SET @TUE = DATEADD(DAY,-2,@DATE)
        SET @WED = DATEADD(DAY,-3,@DATE)
        SET @THU = DATEADD(DAY,2,@DATE)
        SET @FRI = DATEADD(DAY,1,@DATE)
        SET @SAT = DATEADD(DAY,0,@DATE)
    
        SELECT CASE 
            WHEN DATENAME(DW,@DATE) = 'SUNDAY'
            THEN @SUN
            WHEN DATENAME(DW,@DATE) = 'MONDAY'
            THEN @MON
            WHEN DATENAME(DW,@DATE) = 'TUESDAY'
            THEN @TUE
            WHEN DATENAME(DW,@DATE) = 'WEDNESDAY'
            THEN @WED
            WHEN DATENAME(DW,@DATE) = 'THURSDAY'
            THEN @THU
            WHEN DATENAME(DW,@DATE) = 'FRIDAY'
            THEN @FRI
            ELSE @SAT
        END
    
    0 讨论(0)
  • 2021-01-22 19:04

    Try this:

    select dateAdd(dd,7-DATEPART(dw,getDate()),GETDATE())
    

    datePart(dw,...) returns day of the week for current date.

    7 - that number, returns number of days until Saturday

    Add the result to the date to get the next Saturday...

    Similar logic if you need to go back to previous Saturday

    Sql Fiddle: http://www.sqlfiddle.com/#!3/61998/2

    0 讨论(0)
  • 2021-01-22 19:10

    Here's my hint without answering, as you requested:

    Consider date manipulation, using current date (you can use GETDATE() or your date variable), DATEADD() and DATEDIFF() can be used to write this in a couple lines.

    If you just want to simplify your method you could use a CASE statement:

    DECLARE @date DATE = '2013-09-13'
    SELECT CASE WHEN DATENAME(DW,@DATE) = 'SUNDAY' THEN  CAST(DATEADD(DAY,-3,@DATE) AS VARCHAR(12)) + ' IS THE NEAREST SATURDAY'
                WHEN DATENAME(DW,@DATE) = 'MONDAY' THEN CAST(DATEADD(DAY,-2,@DATE) AS VARCHAR(12)) + ' IS THE NEAREST SATURDAY'
                WHEN DATENAME(DW,@DATE) = 'TUESDAY' THEN CAST(DATEADD(DAY,-1,@DATE) AS VARCHAR(12)) + ' IS THE NEAREST SATURDAY'
                WHEN DATENAME(DW,@DATE) = 'WEDNESDAY' THEN CAST(DATEADD(DAY,1,@DATE) AS VARCHAR(12)) + ' IS THE NEAREST SATURDAY'
                WHEN DATENAME(DW,@DATE) = 'THURSDAY' THEN CAST(DATEADD(DAY,2,@DATE) AS VARCHAR(12)) + ' IS THE NEAREST SATURDAY'
                WHEN DATENAME(DW,@DATE) = 'FRIDAY' THEN CAST(DATEADD(DAY,3,@DATE) AS VARCHAR(12)) + ' IS THE NEAREST SATURDAY'
                ELSE CONVERT(NVARCHAR,@DATE) + ' IS SATURDAY'
           END
    

    To clarify on the method I was hinting at and Sparky posted, you need to adjust DATEFIRST to make this work, it works for whichever day is the first day of the week, Saturday is the 7th day of the week, so:

    SET DATEFIRST 7
    DECLARE @date DATE = '2013-09-21'
    SELECT DATEADD(day,7-DATEPART(weekday,@date),@date)
    
    0 讨论(0)
提交回复
热议问题