2

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 messy query. And I was wondering if there is a way to simplify this. Here's my query

DECLARE @DATE DATE
SET @DATE ='2013-09-13'

IF              DATENAME(DW,@DATE) = 'SUNDAY'
    BEGIN 
        SELECT  DATEADD(DAY,-1,@DATE) AS DATE, 'IS THE NEAREST SATURDAY'
    END
ELSE IF         DATENAME(DW,@DATE) = 'MONDAY'
    BEGIN 
        SELECT  DATEADD(DAY,-2,@DATE) AS DATE, 'IS THE NEAREST SATURDAY'
    END
ELSE IF         DATENAME(DW,@DATE) = 'TUESDAY'
    BEGIN 
        SELECT  DATEADD(DAY,-3,@DATE) AS DATE, 'IS THE NEAREST SATURDAY'
    END
ELSE IF         DATENAME(DW,@DATE) = 'WEDNESDAY'
    BEGIN 
        SELECT  DATEADD(DAY,3,@DATE) AS DATE, 'IS THE NEAREST SATURDAY'
    END
ELSE IF         DATENAME(DW,@DATE) = 'THURSDAY'
    BEGIN 
        SELECT  DATEADD(DAY,2,@DATE) AS DATE, 'IS THE NEAREST SATURDAY'
    END
ELSE IF         DATENAME(DW,@DATE) = 'FRIDAY'
    BEGIN 
        SELECT  DATEADD(DAY,1,@DATE) AS DATE, 'IS THE NEAREST SATURDAY'
    END
ELSE IF         DATENAME(DW,@DATE) = 'SATURDAY'
    BEGIN
        SELECT  CONVERT(NVARCHAR,@DATE) + ' IS SATURDAY' AS DATE_DOW
    END

As we can see the query is long and runs multiple IFs to look for TRUE condition. Please don't write the query, just hints. I would like to work on the query myself.

4

3 回答 3

1

根据您的要求,这是我没有回答的提示:

考虑使用当前日期(您可以使用GETDATE()或您的日期变量)进行日期操作,DATEADD()并且DATEDIFF()可以用几行来编写它。

如果您只想简化您的方法,您可以使用以下CASE语句:

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

为了澄清我暗示和 Sparky 发布的方法,您需要进行调整DATEFIRST以使其工作,它适用于一周的第一天,星期六是一周的第 7 天,所以:

SET DATEFIRST 7
DECLARE @date DATE = '2013-09-21'
SELECT DATEADD(day,7-DATEPART(weekday,@date),@date)
于 2013-09-13T16:45:56.423 回答
0

尝试这个:

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

datePart(dw,...)返回当前日期的星期几。

7 - 那个数字,返回到星期六的天数

将结果添加到日期以获得下一个星期六......

如果你需要回到上一个星期六,类似的逻辑

Sql 小提琴:http ://www.sqlfiddle.com/#!3/61998/2

于 2013-09-13T16:57:55.747 回答
0

嗯,不完全是答案,而是另一种选择

    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
于 2013-09-13T17:35:46.413 回答