Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Create list of dates

Author  Topic 

SQLMAKESMECRY
Starting Member

14 Posts

Posted - 2011-03-23 : 09:23:33
Is there a way to create a list of every day of the year by using only today's date and some future date? or some number of days in the future?



Cricket

It's not fun unless I'm pulling my hair out. Weee!

http://strattenramble.blogspot.com/

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2011-03-23 : 09:33:58
Actually, there are several ways

select dateadd(day,number,'20110101')
from master..spt_values
where type = 'p'


Others are more clever, but this works for me.

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

SQLMAKESMECRY
Starting Member

14 Posts

Posted - 2011-03-23 : 09:42:40
quote:
Originally posted by jimf

Actually, there are several ways

select dateadd(day,number,'20110101')
from master..spt_values
where type = 'p'


Others are more clever, but this works for me.

Jim

Everyday I learn something that somebody else already knew


Wow that is a simple one! Thank you!
What database is MAster..spt_values? Is that what its doing? creating a virtual database or is that something in sql that I don't know about?

Cricket

It's not fun unless I'm pulling my hair out. Weee!

http://strattenramble.blogspot.com/
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-03-23 : 10:01:37
master is the main system database for SQL Server, most of the system configuration and procedures are stored there. You should take some time to look around it and get familiar with it, just make sure not to modify anything in it.

Books Online describes what master does in more detail.
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2011-03-23 : 10:01:56
spt_values is a system table in the master database that has various numbers in it, you get 2048 of them for type'P' (0 - 2047)

select * from master.dbo.spt_values

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

SQLMAKESMECRY
Starting Member

14 Posts

Posted - 2011-03-23 : 10:06:43
Thank you both. :)

Cricket

It's not fun unless I'm pulling my hair out. Weee!

http://strattenramble.blogspot.com/
Go to Top of Page

sandeepmittal11
Starting Member

6 Posts

Posted - 2012-12-18 : 01:53:40
Refer this link
[url]http://itdeveloperzone.blogspot.in/2012/11/generate-list-of-dates-in-sql-server.html[/url]

Regards,
Sandeep

My Blog : http://itdeveloperzone.blogspot.in
Go to Top of Page

ScottPletcher
Aged Yak Warrior

550 Posts

Posted - 2012-12-21 : 17:33:56
quote:
Originally posted by jimf

spt_values is a system table in the master database that has various numbers in it, you get 2048 of them for type'P' (0 - 2047)

select * from master.dbo.spt_values

Jim

Everyday I learn something that somebody else already knew




As of today, you get values 0-2047 for type 'P'. You have NO idea if this will be true for the next upgrade to SQL, or if this table will even exist in future versions.

It's far better to use your own in-line CTE to generate numbers "on the fly" or pre-build your own physical tally table and use it. It's extremely poor practice to use something that could easily cause missing data at the next SQL upgrade!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-12-21 : 23:30:33
thats why i use CTE to generate my own number table and then generate dates based on it.

see the logic used in below function

http://visakhm.blogspot.in/2010/02/generating-calendar-table.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -