Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can get Monday for any given date in a week as:</p> <pre><code>DATEADD(d, 1 - DATEPART(dw, @date), @date) </code></pre> <p>and you can write a stored procedure with the following body</p> <pre><code>-- find Monday at that week DECLARE @currentDate SMALLDATETIME SELECT @currentDate = DATEADD(d, 1 - DATEPART(dw, @date), @date) -- create a table and insert the first record DECLARE @weekTable TABLE (Id INT, StartDate SMALLDATETIME, EndDate SMALLDATETIME) INSERT INTO @weekTable VALUES (0, NULL, @currentDate) -- increment the date SELECT @currentDate = DATEADD(d, 1, @currentDate) -- iterate for 7 more weeks DECLARE @id INT SET @id = 1 WHILE @id &lt; 8 BEGIN INSERT INTO @weekTable VALUES (@id, @currentDate, DATEADD(d, 6, @currentDate)) SELECT @currentDate = DATEADD(ww, 1, @currentDate) SET @id = @id + 1 END -- add the last record INSERT INTO @weekTable VALUES (8, @currentDate, NULL) -- select the values SELECT Id 'Week #', StartDate 'Start Date', EndDate 'End Date' FROM @weekTable </code></pre> <p>When I pass</p> <pre><code>@date = '20080827' </code></pre> <p>to this procedure, I get the following</p> <pre><code>Week # Start Date End Date 0 NULL 2008-08-24 00:00:00 1 2008-08-25 00:00:00 2008-08-31 00:00:00 2 2008-09-01 00:00:00 2008-09-07 00:00:00 3 2008-09-08 00:00:00 2008-09-14 00:00:00 4 2008-09-15 00:00:00 2008-09-21 00:00:00 5 2008-09-22 00:00:00 2008-09-28 00:00:00 6 2008-09-29 00:00:00 2008-10-05 00:00:00 7 2008-10-06 00:00:00 2008-10-12 00:00:00 8 2008-10-13 00:00:00 NULL </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload