如何找到给定日期的最近(星期几)

时间:2013-09-13 16:42:52

标签: sql sql-server-2008-r2

我一直在练习查询,而我目前的情况是在给定日期找到最近的星期六。在我得到逻辑后,我想出了一个,什么看起来像一个冗长而凌乱的查询。我想知道是否有办法简化这一点。 这是我的查询

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

我们可以看到查询很长并且运行多个IF来查找TRUE条件。 请不要写查询,只是提示。我想自己处理查询。

3 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

试试这个:

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

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

7 - 该数字,返回星期六的天数

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

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

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

答案 2 :(得分: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