1

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

例如:

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

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

4

8 回答 8

8

如果日期是星期六或星期日,这将为您提供下一个星期一;否则,它将为您提供您提供的日期。

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 问题的解决方案。

于 2012-05-08T15:04:37.580 回答
2

您可以在 T-SQL 中使用DatePart函数。对于每个日期,您可以提取日期部分,如果是星期六,则将日期增加两天。如果是星期日,则将日期增加一天。您可以使用DateAdd来增加日期。

于 2012-05-08T15:01:22.093 回答
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
于 2012-05-08T15:10:06.613 回答
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

于 2012-05-08T15:10:54.440 回答
1

这个怎么样:

SELECT CASE datepart(dw, @date) WHEN 7 THEN @date + 2 WHEN 1 THEN @date + 1 ELSE @date END
于 2012-05-08T15:08:12.910 回答
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
于 2012-05-08T15:10:08.790 回答
1

使用DATEPART和的组合DATEADD

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
于 2012-05-08T15:16:23.627 回答
-1

我在 5 月 8 日扩展了 zimdanen 提供的解决方案,因为开始日期是星期五,但它失败了。

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    
于 2014-04-10T10:16:12.263 回答