Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6950841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:08:33+00:00 2026-05-27T14:08:33+00:00

I’m trying to write a sql query which depending on what the user selects

  • 0

I’m trying to write a sql query which depending on what the user selects will recur every x day every x weeks.
So the user will select that they want the job to recur on tuesdays every 2 weeks
the values that are supplied are

declare @StartDate datetime -- when the job first recurs
declare @recurrenceValue1 int -- amount of weeks
declare @recurrenceValue2 int -- day of week (mon-sun)
declare @NextOcurrance datetime -- when the job will recur

I know how to set it for the amount of weeks:

SET @NextOccurance = (Convert(char(12),@StartDate + (@RecurrenceValue1),106))

But I’m not sure how to make it roll on to the day of the week so if the @startDate is today and it should recur every 2 weeks on a tuesday it will see that 2 weeks today is wednesday so will loop until it know that the day is tuesday and that will be the @NextRecurrance date.

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:08:34+00:00Added an answer on May 27, 2026 at 2:08 pm

    An easy way to add a number of weeks to a date is to use (MSDN DATEADD)

    DATEADD(wk, @StartDate, @recurrenceValue1)
    

    To find out which day of the week the date you are looking at belongs, you can use (MSDN DATEPART)

    DATEPART(dw, @StartDate)
    

    This function uses DATEFIRST to determine which day of the week is the first one (http://msdn.microsoft.com/en-us/library/ms181598.aspx)

    SET DATEFIRST 1 --Where 1 = Monday and 7 = Sunday
    

    So for your problem (DATEFIRST being set to 1 = Monday)..

    SET DATEFIRST 1
    
    declare @StartDate datetime -- when the job first recurs
    declare @recurrenceValue1 int -- amount of weeks
    declare @recurrenceValue2 int -- day of week (mon-sun)
    declare @NextOcurrance datetime -- when the job will recur
    
    
    SET @StartDate = '2011-12-16' -- This is a Friday
    SET @recurrenceValue1 = 2 -- In 2 weeks
    SET @recurrenceValue2 = 2 -- On Tuesday
    SET @NextOcurrance = DATEADD(wk, @recurrenceValue1, @StartDate) -- Add our 2 weeks
    /* Check if our incrementation falls on the correct day - Adjust if needed */
    IF (DATEPART(dw, @NextOcurrance) != @recurrenceValue2) BEGIN
        DECLARE @weekDay int = DATEPART(dw, @NextOcurrance)
        SET @NextOcurrance = DATEADD(dd, ((7 - @weekDay) + @recurrenceValue2), @NextOcurrance) -- Add to @NextOcurrance the number of days missing to be on the requested day of week
    END
    

    The logic for the number of days to add is as follow:
    We have 7 days in a week, how many days does it take to reach the end of this week. Add this number of days to the @recurrenceValue2 (day of week we are looking for).

    PS: I can’t post more than 2 HyperLinks because of my reputation. This is why the DATEFIRST URL is in plain text.


    Here’s some code to allow certain specific date to be handled differently. Though this code is good only for unique dates. If ones needs to skip an entire week for instance, using this code will require to add values for each day of this week to skip. For ranges other than unique days, this code should be modified to handle date ranges or specific weeks and/or day of week.

    CREATE TABLE OccurenceExclusions (
        ExclusionDate DATE not null,
        NumberOfDaysToAdd int not null
    
        PRIMARY KEY (ExclusionDate)
    )
    
    INSERT OccurenceExclusions VALUES ('2012-01-01', 7)
    
    SET @NextOcurrance = DATEADD(dd, COALESCE((SELECT NumberOfDaysToAdd
                               FROM OccurrenceExclusions
                               WHERE ExclusionDate = @NextOcurrance), 0), @NextOcurrance)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I need a function that will clean a strings' special characters. I do NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.