SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Create list of dates
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

SQLMAKESMECRY
Starting Member

14 Posts

Posted - 03/23/2011 :  09:23:33  Show Profile  Reply with Quote
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/

Edited by - SQLMAKESMECRY on 03/23/2011 09:26:17

jimf
Flowing Fount of Yak Knowledge

USA
2869 Posts

Posted - 03/23/2011 :  09:33:58  Show Profile  Reply with Quote
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 - 03/23/2011 :  09:42:40  Show Profile  Reply with Quote
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

USA
15668 Posts

Posted - 03/23/2011 :  10:01:37  Show Profile  Visit robvolk's Homepage  Reply with Quote
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
Flowing Fount of Yak Knowledge

USA
2869 Posts

Posted - 03/23/2011 :  10:01:56  Show Profile  Reply with Quote
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 - 03/23/2011 :  10:06:43  Show Profile  Reply with Quote
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

India
6 Posts

Posted - 12/18/2012 :  01:53:40  Show Profile  Reply with Quote
Refer this link
http://itdeveloperzone.blogspot.in/2012/11/generate-list-of-dates-in-sql-server.html

Regards,
Sandeep

My Blog : http://itdeveloperzone.blogspot.in

Edited by - sandeepmittal11 on 12/18/2012 01:54:52
Go to Top of Page

ScottPletcher
Constraint Violating Yak Guru

USA
372 Posts

Posted - 12/21/2012 :  17:33:56  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 12/21/2012 :  23:30:33  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000