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 6378567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:01:29+00:00 2026-05-25T02:01:29+00:00

I have a table in SQL Server containing a series of weeks by when

  • 0

I have a table in SQL Server containing a series of weeks by when the week begins:

id weekBeginning
1  2011-08-07 00:00:00
2  2011-08-14 00:00:00
3  2011-08-21 00:00:00
4  2011-08-28 00:00:00

I would like a stored proc that, when inputted a date @dt, it outputs the id of the week containing that date, creating a row for that week if one does not exist. There will be no overlap between when weeks begin.)

I have tried the following in SQL Server, but it says I have invalid syntax. What would be the correct way to accomplish this?

declare @weekid int
declare @weekBeginning datetime
while not exists(select @weekid = id from WeekTable where @dt between weekBeginning and date_add(weekBeginning, interval 7 day))
begin
    set @weekBeginning = (select max(weekBeginning)) from WeekTable
    insert WeekTable(weekBeginning) output inserted.id into @weekid values date_add(@weekBeginning, interval 7 day)
end
  • 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-25T02:01:30+00:00Added an answer on May 25, 2026 at 2:01 am

    Now that I’ve re-read the requirements.

    Given an arbitrary date/time in any week after the max week in the table:

    DECLARE @dt DATETIME = '2011-09-15 13:42';
    

    You can run this code:

    -- just declare a variable to avoid expressing twice:
    DECLARE @mw DATETIME = (SELECT MAX(WeekBeginning) FROM WeekTable);
    
    ;WITH n AS
    (
           SELECT DISTINCT n = number
                   FROM [master]..spt_values
                   WHERE number BETWEEN 1 AND DATEDIFF(WEEK, @mw, @dt)
    )
    INSERT dbo.WeekTable(WeekBeginning)
    SELECT DATEADD(WEEK, n, @mw) 
        FROM n ORDER BY n;
    

    No need for a loop, and not sure where you’re learning syntax – date_add is not correct and the interval keyword is not valid either.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a SQL Server table containing Company Name, Address, and Contact name (among
I have a SQL Server 2000 with a table containing an image column. How
I have a SQL Server DB containing a registrations table that I need to
Assuming I have a SQL Server 2005 table with an xml column containing the
I have a table in SQL Server 2000 with a text field containing XML
SQL Server 2005 I have a table containing the following: - [order_id] [index_1] 600020001
I have stored a number of binary files in a SQL Server table. I
I have a table in SQL Server 2005 containing 10000054 records; these records are
I am on SQL Server 2008 and I have a table containing WA metrics
I have a table in SQL server that has the normal tree structure of

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.