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
 Other SQL Server 2008 Topics
 How do I add dates in a table?

Author  Topic 

Lizzie_gurl
Starting Member

11 Posts

Posted - 2013-01-15 : 17:15:28
Hi,

Hoping you guys can help me.
I am trying to add the dates from 01-01-2013 to 12-31-2013 in a table where the only column is the "date"
How do I do these?

Result should be like this:
**************
Date
2013-01-01
2013-01-02
2013-01-03
2013-01-04
.......
2013-12-31
***************
Please help.

Thanks!

Lizzie :-)

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-01-15 : 18:56:19
If you have a numbers table in your database, use that to generate the dates; if you don't have one, you can use master..spt_values to sequential numbers - example:
INSERT INTO YourSingleColumnTable
(YourColumnName)
SELECT
DATEADD(dd,number,'20130101') AS [Date]
FROM
master..spt_values
WHERE
type = 'P'
AND DATEADD(dd,number,'20130101') < '20140101'
Go to Top of Page

Lizzie_gurl
Starting Member

11 Posts

Posted - 2013-01-16 : 12:04:53
it works...amazing! thank you so much for the help James K..."you rock" :-)

Lizzie :-)
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-01-16 : 14:05:18
You are welcome Lizzie - glad to be of help.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-01-16 : 23:54:36
I dont prefer using code with internal tables like master..spt_values especially in production as we cant guarantee implementation to be same across versions. I mostly use my own number table for similar situations


;With Date_table (dt)
AS
(
SELECT CONVERT(datetime,'20130101')
UNION ALL
SELECT dt+1
FROM Date_table
WHERE dt+1 <= '20131231'
)
SELECT *
FROM date_table
OPTION (MAXRECURSION 0)


output
------------------------------------
dt
2013-01-01 00:00:00.000
2013-01-02 00:00:00.000
2013-01-03 00:00:00.000
2013-01-04 00:00:00.000
2013-01-05 00:00:00.000
2013-01-06 00:00:00.000
2013-01-07 00:00:00.000
2013-01-08 00:00:00.000
2013-01-09 00:00:00.000
2013-01-10 00:00:00.000
2013-01-11 00:00:00.000
2013-01-12 00:00:00.000
2013-01-13 00:00:00.000
2013-01-14 00:00:00.000
2013-01-15 00:00:00.000
2013-01-16 00:00:00.000
2013-01-17 00:00:00.000
2013-01-18 00:00:00.000
2013-01-19 00:00:00.000
2013-01-20 00:00:00.000
2013-01-21 00:00:00.000
2013-01-22 00:00:00.000
2013-01-23 00:00:00.000
2013-01-24 00:00:00.000
2013-01-25 00:00:00.000
2013-01-26 00:00:00.000
2013-01-27 00:00:00.000
2013-01-28 00:00:00.000
2013-01-29 00:00:00.000
2013-01-30 00:00:00.000
2013-01-31 00:00:00.000
2013-02-01 00:00:00.000
2013-02-02 00:00:00.000
2013-02-03 00:00:00.000
2013-02-04 00:00:00.000
2013-02-05 00:00:00.000
2013-02-06 00:00:00.000
2013-02-07 00:00:00.000
2013-02-08 00:00:00.000
2013-02-09 00:00:00.000
2013-02-10 00:00:00.000
2013-02-11 00:00:00.000
2013-02-12 00:00:00.000
2013-02-13 00:00:00.000
2013-02-14 00:00:00.000
2013-02-15 00:00:00.000
2013-02-16 00:00:00.000
2013-02-17 00:00:00.000
2013-02-18 00:00:00.000
2013-02-19 00:00:00.000
2013-02-20 00:00:00.000
2013-02-21 00:00:00.000
2013-02-22 00:00:00.000
2013-02-23 00:00:00.000
2013-02-24 00:00:00.000
2013-02-25 00:00:00.000
2013-02-26 00:00:00.000
2013-02-27 00:00:00.000
2013-02-28 00:00:00.000
2013-03-01 00:00:00.000
2013-03-02 00:00:00.000
2013-03-03 00:00:00.000
2013-03-04 00:00:00.000
2013-03-05 00:00:00.000
2013-03-06 00:00:00.000
2013-03-07 00:00:00.000
2013-03-08 00:00:00.000
2013-03-09 00:00:00.000
2013-03-10 00:00:00.000
2013-03-11 00:00:00.000
2013-03-12 00:00:00.000
2013-03-13 00:00:00.000
2013-03-14 00:00:00.000
2013-03-15 00:00:00.000
2013-03-16 00:00:00.000
2013-03-17 00:00:00.000
2013-03-18 00:00:00.000
2013-03-19 00:00:00.000
2013-03-20 00:00:00.000
2013-03-21 00:00:00.000
2013-03-22 00:00:00.000
2013-03-23 00:00:00.000
2013-03-24 00:00:00.000
2013-03-25 00:00:00.000
2013-03-26 00:00:00.000
2013-03-27 00:00:00.000
2013-03-28 00:00:00.000
2013-03-29 00:00:00.000
2013-03-30 00:00:00.000
2013-03-31 00:00:00.000
2013-04-01 00:00:00.000
2013-04-02 00:00:00.000
2013-04-03 00:00:00.000
2013-04-04 00:00:00.000
2013-04-05 00:00:00.000
2013-04-06 00:00:00.000
2013-04-07 00:00:00.000
2013-04-08 00:00:00.000
2013-04-09 00:00:00.000
2013-04-10 00:00:00.000
2013-04-11 00:00:00.000
2013-04-12 00:00:00.000
2013-04-13 00:00:00.000
2013-04-14 00:00:00.000
2013-04-15 00:00:00.000
2013-04-16 00:00:00.000
2013-04-17 00:00:00.000
2013-04-18 00:00:00.000
2013-04-19 00:00:00.000
2013-04-20 00:00:00.000
2013-04-21 00:00:00.000
2013-04-22 00:00:00.000
2013-04-23 00:00:00.000
2013-04-24 00:00:00.000
2013-04-25 00:00:00.000
2013-04-26 00:00:00.000
2013-04-27 00:00:00.000
2013-04-28 00:00:00.000
2013-04-29 00:00:00.000
2013-04-30 00:00:00.000
2013-05-01 00:00:00.000
2013-05-02 00:00:00.000
2013-05-03 00:00:00.000
2013-05-04 00:00:00.000
2013-05-05 00:00:00.000
2013-05-06 00:00:00.000
2013-05-07 00:00:00.000
2013-05-08 00:00:00.000
2013-05-09 00:00:00.000
2013-05-10 00:00:00.000
2013-05-11 00:00:00.000
2013-05-12 00:00:00.000
2013-05-13 00:00:00.000
2013-05-14 00:00:00.000
2013-05-15 00:00:00.000
2013-05-16 00:00:00.000
2013-05-17 00:00:00.000
2013-05-18 00:00:00.000
2013-05-19 00:00:00.000
2013-05-20 00:00:00.000
2013-05-21 00:00:00.000
2013-05-22 00:00:00.000
2013-05-23 00:00:00.000
2013-05-24 00:00:00.000
2013-05-25 00:00:00.000
2013-05-26 00:00:00.000
2013-05-27 00:00:00.000
2013-05-28 00:00:00.000
2013-05-29 00:00:00.000
2013-05-30 00:00:00.000
2013-05-31 00:00:00.000
2013-06-01 00:00:00.000
2013-06-02 00:00:00.000
2013-06-03 00:00:00.000
2013-06-04 00:00:00.000
2013-06-05 00:00:00.000
2013-06-06 00:00:00.000
2013-06-07 00:00:00.000
2013-06-08 00:00:00.000
2013-06-09 00:00:00.000
2013-06-10 00:00:00.000
2013-06-11 00:00:00.000
2013-06-12 00:00:00.000
2013-06-13 00:00:00.000
2013-06-14 00:00:00.000
2013-06-15 00:00:00.000
2013-06-16 00:00:00.000
2013-06-17 00:00:00.000
2013-06-18 00:00:00.000
2013-06-19 00:00:00.000
2013-06-20 00:00:00.000
2013-06-21 00:00:00.000
2013-06-22 00:00:00.000
2013-06-23 00:00:00.000
2013-06-24 00:00:00.000
2013-06-25 00:00:00.000
2013-06-26 00:00:00.000
2013-06-27 00:00:00.000
2013-06-28 00:00:00.000
2013-06-29 00:00:00.000
2013-06-30 00:00:00.000
2013-07-01 00:00:00.000
2013-07-02 00:00:00.000
2013-07-03 00:00:00.000
2013-07-04 00:00:00.000
2013-07-05 00:00:00.000
2013-07-06 00:00:00.000
2013-07-07 00:00:00.000
2013-07-08 00:00:00.000
2013-07-09 00:00:00.000
2013-07-10 00:00:00.000
2013-07-11 00:00:00.000
2013-07-12 00:00:00.000
2013-07-13 00:00:00.000
2013-07-14 00:00:00.000
2013-07-15 00:00:00.000
2013-07-16 00:00:00.000
2013-07-17 00:00:00.000
2013-07-18 00:00:00.000
2013-07-19 00:00:00.000
2013-07-20 00:00:00.000
2013-07-21 00:00:00.000
2013-07-22 00:00:00.000
2013-07-23 00:00:00.000
2013-07-24 00:00:00.000
2013-07-25 00:00:00.000
2013-07-26 00:00:00.000
2013-07-27 00:00:00.000
2013-07-28 00:00:00.000
2013-07-29 00:00:00.000
2013-07-30 00:00:00.000
2013-07-31 00:00:00.000
2013-08-01 00:00:00.000
2013-08-02 00:00:00.000
2013-08-03 00:00:00.000
2013-08-04 00:00:00.000
2013-08-05 00:00:00.000
2013-08-06 00:00:00.000
2013-08-07 00:00:00.000
2013-08-08 00:00:00.000
2013-08-09 00:00:00.000
2013-08-10 00:00:00.000
2013-08-11 00:00:00.000
2013-08-12 00:00:00.000
2013-08-13 00:00:00.000
2013-08-14 00:00:00.000
2013-08-15 00:00:00.000
2013-08-16 00:00:00.000
2013-08-17 00:00:00.000
2013-08-18 00:00:00.000
2013-08-19 00:00:00.000
2013-08-20 00:00:00.000
2013-08-21 00:00:00.000
2013-08-22 00:00:00.000
2013-08-23 00:00:00.000
2013-08-24 00:00:00.000
2013-08-25 00:00:00.000
2013-08-26 00:00:00.000
2013-08-27 00:00:00.000
2013-08-28 00:00:00.000
2013-08-29 00:00:00.000
2013-08-30 00:00:00.000
2013-08-31 00:00:00.000
2013-09-01 00:00:00.000
2013-09-02 00:00:00.000
2013-09-03 00:00:00.000
2013-09-04 00:00:00.000
2013-09-05 00:00:00.000
2013-09-06 00:00:00.000
2013-09-07 00:00:00.000
2013-09-08 00:00:00.000
2013-09-09 00:00:00.000
2013-09-10 00:00:00.000
2013-09-11 00:00:00.000
2013-09-12 00:00:00.000
2013-09-13 00:00:00.000
2013-09-14 00:00:00.000
2013-09-15 00:00:00.000
2013-09-16 00:00:00.000
2013-09-17 00:00:00.000
2013-09-18 00:00:00.000
2013-09-19 00:00:00.000
2013-09-20 00:00:00.000
2013-09-21 00:00:00.000
2013-09-22 00:00:00.000
2013-09-23 00:00:00.000
2013-09-24 00:00:00.000
2013-09-25 00:00:00.000
2013-09-26 00:00:00.000
2013-09-27 00:00:00.000
2013-09-28 00:00:00.000
2013-09-29 00:00:00.000
2013-09-30 00:00:00.000
2013-10-01 00:00:00.000
2013-10-02 00:00:00.000
2013-10-03 00:00:00.000
2013-10-04 00:00:00.000
2013-10-05 00:00:00.000
2013-10-06 00:00:00.000
2013-10-07 00:00:00.000
2013-10-08 00:00:00.000
2013-10-09 00:00:00.000
2013-10-10 00:00:00.000
2013-10-11 00:00:00.000
2013-10-12 00:00:00.000
2013-10-13 00:00:00.000
2013-10-14 00:00:00.000
2013-10-15 00:00:00.000
2013-10-16 00:00:00.000
2013-10-17 00:00:00.000
2013-10-18 00:00:00.000
2013-10-19 00:00:00.000
2013-10-20 00:00:00.000
2013-10-21 00:00:00.000
2013-10-22 00:00:00.000
2013-10-23 00:00:00.000
2013-10-24 00:00:00.000
2013-10-25 00:00:00.000
2013-10-26 00:00:00.000
2013-10-27 00:00:00.000
2013-10-28 00:00:00.000
2013-10-29 00:00:00.000
2013-10-30 00:00:00.000
2013-10-31 00:00:00.000
2013-11-01 00:00:00.000
2013-11-02 00:00:00.000
2013-11-03 00:00:00.000
2013-11-04 00:00:00.000
2013-11-05 00:00:00.000
2013-11-06 00:00:00.000
2013-11-07 00:00:00.000
2013-11-08 00:00:00.000
2013-11-09 00:00:00.000
2013-11-10 00:00:00.000
2013-11-11 00:00:00.000
2013-11-12 00:00:00.000
2013-11-13 00:00:00.000
2013-11-14 00:00:00.000
2013-11-15 00:00:00.000
2013-11-16 00:00:00.000
2013-11-17 00:00:00.000
2013-11-18 00:00:00.000
2013-11-19 00:00:00.000
2013-11-20 00:00:00.000
2013-11-21 00:00:00.000
2013-11-22 00:00:00.000
2013-11-23 00:00:00.000
2013-11-24 00:00:00.000
2013-11-25 00:00:00.000
2013-11-26 00:00:00.000
2013-11-27 00:00:00.000
2013-11-28 00:00:00.000
2013-11-29 00:00:00.000
2013-11-30 00:00:00.000
2013-12-01 00:00:00.000
2013-12-02 00:00:00.000
2013-12-03 00:00:00.000
2013-12-04 00:00:00.000
2013-12-05 00:00:00.000
2013-12-06 00:00:00.000
2013-12-07 00:00:00.000
2013-12-08 00:00:00.000
2013-12-09 00:00:00.000
2013-12-10 00:00:00.000
2013-12-11 00:00:00.000
2013-12-12 00:00:00.000
2013-12-13 00:00:00.000
2013-12-14 00:00:00.000
2013-12-15 00:00:00.000
2013-12-16 00:00:00.000
2013-12-17 00:00:00.000
2013-12-18 00:00:00.000
2013-12-19 00:00:00.000
2013-12-20 00:00:00.000
2013-12-21 00:00:00.000
2013-12-22 00:00:00.000
2013-12-23 00:00:00.000
2013-12-24 00:00:00.000
2013-12-25 00:00:00.000
2013-12-26 00:00:00.000
2013-12-27 00:00:00.000
2013-12-28 00:00:00.000
2013-12-29 00:00:00.000
2013-12-30 00:00:00.000
2013-12-31 00:00:00.000



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

Go to Top of Page

enjoydiablo3
Starting Member

4 Posts

Posted - 2013-01-17 : 04:58:39
unspammed
Go to Top of Page
   

- Advertisement -