Next WeekDay

SQL 321 Reputation points
2020-09-29T17:22:39.037+00:00

Hi:

I would like to get next weekday in variable (exclude Saturday and Sunday). I am using this query, but on Friday it will give me Saturday. I want it to be Monday.

DECLARE @NextDay DATE = (SELECT CAST(dateadd(d, 1, GETDATE()) AS DATE))
SELECT @NextDay

Thanks!

Developer technologies | Transact-SQL
{count} votes

4 answers

Sort by: Most helpful
  1. Olaf Helper 47,516 Reputation points
    2020-10-02T06:16:27.013+00:00

    get next weekday in variable (exclude Saturday and Sunday)

    What about holidays? If you want to filter then also out, create a date table, example:
    https://www.mssqltips.com/sqlservertip/4054/creating-a-date-dimension-or-calendar-table-in-sql-server/

    1 person found this answer helpful.

  2. SQL 321 Reputation points
    2020-09-29T17:38:42.057+00:00

    Nevermind..... I think it is this

    DECLARE @CurrDay DATE ='20200925'
    SELECT DATEADD(dd,CASE WHEN DATEDIFF(dd,0,@CurrDay)%7 > 3 THEN 7-DATEDIFF(dd,0,@CurrDay)%7 ELSE 1 END,@CurrDay)

    0 comments No comments

  3. MelissaMa-MSFT 24,221 Reputation points
    2020-09-30T01:06:28.947+00:00

    Hi @SQL

    Thank you so much for posting here.

    Please also refer below:

    DECLARE @CurrDay DATE ='20200925'  
    SELECT  DATEADD(DAY, CASE DATENAME(WEEKDAY, @CurrDay)   
     WHEN 'Friday' THEN 3   
     WHEN 'Saturday' THEN 2   
     ELSE 1 END, DATEDIFF(DAY, 0, @CurrDay))  
    

    Best regards
    Melissa


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  4. Tom Phillips 17,771 Reputation points
    2020-10-02T16:19:29.097+00:00

    You should use a calendar table. See "udf_AddBusinessDays":

    https://weblogs.sqlteam.com/dang/2010/07/19/calendar-table-and-datetime-functions/

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.