Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use what's called a numbers table. Just create a table with as many rows as there are days between your dates, number them sequentially.</p> <p>Here's a pretty slick way to create a numbers table in SQL 2008, may also work in 2005: <a href="http://archive.msdn.microsoft.com/SQLExamples/Wiki/View.aspx?title=NumbersTable" rel="nofollow">http://archive.msdn.microsoft.com/SQLExamples/Wiki/View.aspx?title=NumbersTable</a></p> <p>Alternatively, you just create a table with an identity and then insert TOP x rows into it.</p> <p>From there you can compute the rest</p> <pre><code>number 1 DateAdd(dd, '2011/07/20', number) DatePart(dw, DateAdd(dd, '2011/07/20', number)) 2 DateAdd(dd, '2011/07/20', number) DatePart(dw, DateAdd(dd, '2011/07/20', number)) 3 DateAdd(dd, '2011/07/20', number) DatePart(dw, DateAdd(dd, '2011/07/20', number)) 4 DateAdd(dd, '2011/07/20', number) DatePart(dw, DateAdd(dd, '2011/07/20', number)) 5 DateAdd(dd, '2011/07/20', number) DatePart(dw, DateAdd(dd, '2011/07/20', number)) 6 DateAdd(dd, '2011/07/20', number) DatePart(dw, DateAdd(dd, '2011/07/20', number)) 7 DateAdd(dd, '2011/07/20', number) DatePart(dw, DateAdd(dd, '2011/07/20', number)) </code></pre> <p>Join that table to your original results, and then insert the product into your final table.</p> <p>Query:</p> <pre><code>SELECT TOP 5000 IDENTITY( INT, 0, 1 ) AS N INTO Number FROM sys.objects a, sys.objects b, sys.objects c SELECT N, DATEADD(dd, N, '7/20/2011') AS Date, DATEPART(dw, DATEADD(dd, N, '7/20/2011')) AS DayofWeek FROM Number WHERE DATEADD(dd, N, '7/20/2011') BETWEEN '7/20/2011' AND '8/17/2011' </code></pre> <p>Result:</p> <pre><code>N Date DayofWeek ----------- ----------------------- ----------- 0 2011-07-20 00:00:00.000 4 1 2011-07-21 00:00:00.000 5 2 2011-07-22 00:00:00.000 6 3 2011-07-23 00:00:00.000 7 4 2011-07-24 00:00:00.000 1 5 2011-07-25 00:00:00.000 2 6 2011-07-26 00:00:00.000 3 7 2011-07-27 00:00:00.000 4 8 2011-07-28 00:00:00.000 5 9 2011-07-29 00:00:00.000 6 10 2011-07-30 00:00:00.000 7 11 2011-07-31 00:00:00.000 1 12 2011-08-01 00:00:00.000 2 13 2011-08-02 00:00:00.000 3 14 2011-08-03 00:00:00.000 4 15 2011-08-04 00:00:00.000 5 16 2011-08-05 00:00:00.000 6 17 2011-08-06 00:00:00.000 7 18 2011-08-07 00:00:00.000 1 19 2011-08-08 00:00:00.000 2 20 2011-08-09 00:00:00.000 3 21 2011-08-10 00:00:00.000 4 22 2011-08-11 00:00:00.000 5 23 2011-08-12 00:00:00.000 6 24 2011-08-13 00:00:00.000 7 25 2011-08-14 00:00:00.000 1 26 2011-08-15 00:00:00.000 2 27 2011-08-16 00:00:00.000 3 28 2011-08-17 00:00:00.000 4 </code></pre>
    singulars
    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. 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