Generate a list of all the dates in October 2012

Question

Produce a list of all the dates in October 2012. They can be output as a timestamp (with time set to midnight) or a date.

Expected Results

ts
Mon Oct 01 2012 00:00:00 GMT+0000 (Coordinated Universal Time)
Tue Oct 02 2012 00:00:00 GMT+0000 (Coordinated Universal Time)
Wed Oct 03 2012 00:00:00 GMT+0000 (Coordinated Universal Time)
Thu Oct 04 2012 00:00:00 GMT+0000 (Coordinated Universal Time)
Fri Oct 05 2012 00:00:00 GMT+0000 (Coordinated Universal Time)
Sat Oct 06 2012 00:00:00 GMT+0000 (Coordinated Universal Time)
Sun Oct 07 2012 00:00:00 GMT+0000 (Coordinated Universal Time)
Mon Oct 08 2012 00:00:00 GMT+0000 (Coordinated Universal Time)
Tue Oct 09 2012 00:00:00 GMT+0000 (Coordinated Universal Time)
Wed Oct 10 2012 00:00:00 GMT+0000 (Coordinated Universal Time)
Thu Oct 11 2012 00:00:00 GMT+0000 (Coordinated Universal Time)
Fri Oct 12 2012 00:00:00 GMT+0000 (Coordinated Universal Time)
Sat Oct 13 2012 00:00:00 GMT+0000 (Coordinated Universal Time)
Sun Oct 14 2012 00:00:00 GMT+0000 (Coordinated Universal Time)
Mon Oct 15 2012 00:00:00 GMT+0000 (Coordinated Universal Time)
Tue Oct 16 2012 00:00:00 GMT+0000 (Coordinated Universal Time)
Wed Oct 17 2012 00:00:00 GMT+0000 (Coordinated Universal Time)
Thu Oct 18 2012 00:00:00 GMT+0000 (Coordinated Universal Time)
Fri Oct 19 2012 00:00:00 GMT+0000 (Coordinated Universal Time)
Sat Oct 20 2012 00:00:00 GMT+0000 (Coordinated Universal Time)
Sun Oct 21 2012 00:00:00 GMT+0000 (Coordinated Universal Time)
Mon Oct 22 2012 00:00:00 GMT+0000 (Coordinated Universal Time)
Tue Oct 23 2012 00:00:00 GMT+0000 (Coordinated Universal Time)
Wed Oct 24 2012 00:00:00 GMT+0000 (Coordinated Universal Time)
Thu Oct 25 2012 00:00:00 GMT+0000 (Coordinated Universal Time)
Fri Oct 26 2012 00:00:00 GMT+0000 (Coordinated Universal Time)
Sat Oct 27 2012 00:00:00 GMT+0000 (Coordinated Universal Time)
Sun Oct 28 2012 00:00:00 GMT+0000 (Coordinated Universal Time)
Mon Oct 29 2012 00:00:00 GMT+0000 (Coordinated Universal Time)
Tue Oct 30 2012 00:00:00 GMT+0000 (Coordinated Universal Time)
Wed Oct 31 2012 00:00:00 GMT+0000 (Coordinated Universal Time)

Your Answer

Your Results

Loading database...

Answers and Discussion

select generate_series(timestamp '2012-10-01', timestamp '2012-10-31', interval '1 day') as ts;

One of the best features of Postgres over other DBs is a simple function called GENERATE_SERIES. This function allows you to generate a list of dates or numbers, specifying a start, an end, and an increment value. It's extremely useful for situations where you want to output, say, sales per day over the course of a month. A typical way to do that on a table containing a list of sales might be to use a SUM aggregation, grouping by the date and product type. Unfortunately, this approach has a flaw: if there are no sales for a given day, it won't show up! To make it work properly, you need to left join from a sequential list of timestamps to the aggregated data to fill in the blank spaces.

On other database systems, it's not uncommon to keep a 'calendar table' full of dates, with which you can perform these joins. Alternatively, on some systems you can write an analogue to generate_series using recursive CTEs. Fortunately for us, Postgres makes our lives a lot easier!