if条件仅在工作日执行,即mon-fri

时间:2016-03-21 10:47:51

标签: sql sql-server sql-server-2012

我想要一个只在工作日执行的条件, 变量@Executedate是int类型,只占用datetime的日期部分,如果@Executeday是星期六或星期日,它应该重置为星期一。

这是一段代码,

Declare @Executeday int      

declare @time varchar(200)

set @time =  convert(varchar(8), getdate(), 108)

if day(getdate()) < 16                          
begin 

set @Executeday = (select top 1 Description from cts_configuration(nolock) where name like 'ReminderMailerFirstFortNightDay')

end
else                          
begin         
set @Executeday = (select top 1 Description from cts_configuration(nolock) where name like 'ReminderMailerSecondFortNightDay')

 end

if (day(getdate())=@Executeday) or (day(getdate())=@Executeday+2) or (day(getdate())=@Executeday+4)

begin

if(@time between '10:00:00' and '11:00:00') 
begin

我想在这里使用if条件......(如果它是坐着或太阳,那将@executeday重置为星期一)

1 个答案:

答案 0 :(得分:0)

DECLARE @ExecuteDay AS int = 7;

DECLARE @Today AS date = GETDATE();
DECLARE @ExecuteDate AS date = DATEADD(day, @ExecuteDay - DATEPART(day, @Today), @Today);
DECLARE @WeekDay AS int = (@@DATEFIRST + DATEPART(WEEKDAY, @ExecuteDate)) % 7;

IF @WeekDay IN (0, 1)
    SET @ExecuteDate = DATEADD(day, 2 - @WeekDay, @ExecuteDate)

PRINT @ExecuteDate