How to get Previous business day in a week with that of current Business Day using sql server

后端 未结 8 2174
情歌与酒
情歌与酒 2020-12-14 09:00

i have an ssis Package which runs on business days (mon-Fri). if i receive file on tuesday , background(DB), it takes previous business day date and does some transactions.

8条回答
  •  被撕碎了的回忆
    2020-12-14 09:37

    thanks for the tips above, I had a slight variant on the query in that my user needed all values for the previous business date. For example, today is a Monday so he needs everything between last Friday at midnight through to Saturday at Midnight. I did this using a combo of the above, and "between", just if anyone is interested. I'm not a massive techie.

    -- Declare a variable for the start and end dates.
    declare @StartDate as datetime 
    declare @EndDate as datetime 
    
    SELECT  @StartDate = DATEADD(DAY, CASE DATENAME(WEEKDAY, GETDATE()) 
    WHEN 'Sunday' THEN -2 
    WHEN 'Monday' THEN -3 
        ELSE -1 END, DATEDIFF(DAY, 0, GETDATE()))
    select @EndDate = @StartDate + 1 
    select @StartDate , @EndDate 
    -- Later on in the query use "between"
    and mydate between @StartDate and @EndDate
    

提交回复
热议问题