在SQL中将日期移动到星期一

时间:2012-05-08 14:58:14

标签: sql sql-server tsql

我需要在TSQL中创建一个函数,如果传递的日期只是星期六或星期日,它会将日期移动到星期一。

例如:

  • 2012年5月6日应该返回5/7/2012
  • 2012年5月12日应该返回5/14/2012

如果日期不是星期六或星期日,请返回输入日期

8 个答案:

答案 0 :(得分:6)

如果日期是星期六或星期日,这将给你星期一;否则,它会给你你提供的日期。

DECLARE @Date DATETIME; SET @Date = GETDATE();

SELECT
    CASE (@@DATEFIRST + DATEPART(dw, @Date)) % 7
        WHEN 0 THEN DATEADD(day, 2, @Date) -- Saturday
        WHEN 1 THEN DATEADD(day, 1, @Date) -- Sunday
        ELSE @Date
    END AS Weekday

编辑:包含 Conrad Frix 对@@ DATEFIRST问题的解决方案。

答案 1 :(得分:2)

您可以在T-SQL中使用DatePart功能。对于每个日期,您可以提取日期组件,如果是星期六,则将日期增加两天。如果是星期天,请将日期增加一天。您可以使用DateAdd来增加日期。

答案 2 :(得分:2)

取决于您的SET DATEFIRST选项(美国英语默认值为7,星期日= 1):

CASE
    WHEN DATEPART(weekday, @dt) = 1 -- Sunday
        THEN DATEADD(day, 1, @dt)
    WHEN DATEPART(weekday, @dt) = 7 -- Saturday
        THEN DATEADD(day, 2, @dt)
    ELSE
        @dt 
END

答案 3 :(得分:2)

不依赖set datefirst

的版本
select dateadd(day,
               case datediff(day, '17530101', @D) % 7
                 when 5 then 2
                 when 6 then 1
                 else 0 
               end,
               @D)

同样的事情可能比较漂亮。

select case datediff(day, '17530101', @D) % 7 
         when 5 then dateadd(day, 2, @D) 
         when 6 then dateadd(day, 1, @D) 
         else @D 
       end

常量17530101是星期一,适用于datetime。如果您使用date,则可以改为使用00010101

答案 4 :(得分:1)

这个怎么样:

SELECT CASE datepart(dw, @date) WHEN 7 THEN @date + 2 WHEN 1 THEN @date + 1 ELSE @date END

答案 5 :(得分:1)

这应该为你做。基本上在星期六增加2天或在星期日增加1天将样本日期移至星期一

DECLARE @Temp TABLE
(
    dtSample DATETIME
)

INSERT INTO @Temp VALUES ('5/1/2012')
INSERT INTO @Temp VALUES ('5/2/2012')
INSERT INTO @Temp VALUES ('5/3/2012')
INSERT INTO @Temp VALUES ('5/4/2012')
INSERT INTO @Temp VALUES ('5/5/2012')
INSERT INTO @Temp VALUES ('5/6/2012')
INSERT INTO @Temp VALUES ('5/7/2012')
INSERT INTO @Temp VALUES ('5/8/2012')
INSERT INTO @Temp VALUES ('5/9/2012')
INSERT INTO @Temp VALUES ('5/10/2012')
INSERT INTO @Temp VALUES ('5/11/2012')
INSERT INTO @Temp VALUES ('5/12/2012')
INSERT INTO @Temp VALUES ('5/13/2012')
INSERT INTO @Temp VALUES ('5/14/2012')

SELECT 
    dtSample
    ,CASE WHEN DATEPART(WEEKDAY, dtSample) = 7 
        THEN dtSample + 2
        ELSE 
            CASE WHEN DATEPART(WEEKDAY, dtSample) = 1
                THEN dtSample + 1
                ELSE dtSample
            END
     END AS dtConverted             
FROM @Temp

结果:

dtSample    dtConverted
2012-05-01 00:00:00.000 2012-05-01 00:00:00.000
2012-05-02 00:00:00.000 2012-05-02 00:00:00.000
2012-05-03 00:00:00.000 2012-05-03 00:00:00.000
2012-05-04 00:00:00.000 2012-05-04 00:00:00.000
2012-05-05 00:00:00.000 2012-05-07 00:00:00.000
2012-05-06 00:00:00.000 2012-05-07 00:00:00.000
2012-05-07 00:00:00.000 2012-05-07 00:00:00.000
2012-05-08 00:00:00.000 2012-05-08 00:00:00.000
2012-05-09 00:00:00.000 2012-05-09 00:00:00.000
2012-05-10 00:00:00.000 2012-05-10 00:00:00.000
2012-05-11 00:00:00.000 2012-05-11 00:00:00.000
2012-05-12 00:00:00.000 2012-05-14 00:00:00.000
2012-05-13 00:00:00.000 2012-05-14 00:00:00.000
2012-05-14 00:00:00.000 2012-05-14 00:00:00.000

答案 6 :(得分:1)

使用DATEPARTDATEADD的组合:

select  date
,       CASE DATEPART(weekday, date)            
            WHEN 6 THEN DATEADD(day, 2, date) -- Saturday
            WHEN 7 THEN DATEADD(day, 1, date) -- Sunday
            ELSE date 
        END AS NewDate
from @dates
order by date

示例数据:

declare @dates table(date datetime);
insert into @dates values('20120401');
insert into @dates values('20120402');
insert into @dates values('20120403');
insert into @dates values('20120404');
insert into @dates values('20120405');
insert into @dates values('20120406');
insert into @dates values('20120407');
insert into @dates values('20120408');
insert into @dates values('20120409');
insert into @dates values('20120410');

结果:

date                    NewDate
2012-04-01 00:00:00.000 2012-04-02 00:00:00.000
2012-04-02 00:00:00.000 2012-04-02 00:00:00.000
2012-04-03 00:00:00.000 2012-04-03 00:00:00.000
2012-04-04 00:00:00.000 2012-04-04 00:00:00.000
2012-04-05 00:00:00.000 2012-04-05 00:00:00.000
2012-04-06 00:00:00.000 2012-04-06 00:00:00.000
2012-04-07 00:00:00.000 2012-04-09 00:00:00.000
2012-04-08 00:00:00.000 2012-04-09 00:00:00.000
2012-04-09 00:00:00.000 2012-04-09 00:00:00.000
2012-04-10 00:00:00.000 2012-04-10 00:00:00.000

答案 7 :(得分:-1)

我延长了zimdanen在5月8日提供的解决方案,因为当开始日期是星期五失败时。

DECLARE @FromDate DATETIME

BEGIN

SET @FromDate='04/07/2014'

SELECT
     @FromDate as [StartDate]
   , CASE (@@DATEFIRST + DATEPART(dw, (DATEADD(DD,3,@FromDate)))) % 7
        WHEN 0 THEN DATEADD(day, 2, (DATEADD(DD,3,@FromDate))) -- Saturday
        WHEN 1 THEN 
                    CASE    
                        WHEN DATEPART(dw,@FromDate)=5 Then DATEADD(DD,5,@FromDate) -- Thursday
                        ELSE DATEADD(day, 1, (DATEADD(DD,3,@FromDate))) -- Sunday  
                    END                       

        ELSE 
                    CASE    
                        WHEN DATEPART(dw,@FromDate)=6 Then DATEADD(DD,5,@FromDate) -- Friday
                        ELSE DATEADD(DD,3,@FromDate)
                    END 

    END AS Weekday



END