Fastest date tally table

This is by far the fastest way for a date tally table to create:

select
convert(nvarchar(10), dateadd(day, row_number() over (order by (select 1)) -1 , getdate()) , 104)
from master..spt_values

And the best part it can be used with any table.

Happy querying.

Advertisements

2 thoughts on “Fastest date tally table

  1. Very ugly:
    1. you need the permission to access master..spt_values – which users do not have generally
    2. you only get a couple of rows (~2000), so very limited
    3. you are not sure how many rows it returns (differerent counts on different SQL-Servers)

    Like

  2. Hi MiDi,

    1. you can use any table, whatsoever. if you analyze the code you will notice, there is no reference to table master..spt_values. I used this table as a sample, because it is available in any instance of SQL Server! if you are having permission problems, explaing to your users they can use this with any table.
    2. you can simply add following “cross join master..spt_values as sp2” at the end of query, but I am sure you will never use that many future dates!
    3. use cross join and limit the end/stop date!

    Enjoy!

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s