I have over the last couple of days been working with Jamie Thomson on a sql query to return a daily value inferred from periodic data readings throughout a month. One of the requirements to creating this query was to generate a list of days in the month. Sounds pretty easy except that we couldn't use procedural logic (such as a cursor), so it actually took a little bit of time to figure it out hence I thought it was worthwhile sharing. My thanks go to 'Hampus' for their blog where I found out how to do this.
The database we are querying is in Oracle, so the following sql has been written in TOAD, though I notice Jamie has already posted a corresponding version for SQL Server and he has bullied/cajouled me into posting the Oracle version, which incidentally is much simpler.
1 select to_date('&date','MM YYYY')-1 + level as DateRange
2 from dual
3 where (to_date('&date','MM YYYY')-1+level) <= last_day(to_date('&Date','MM YYYY'))
4 connect by level<=31
5 ;
Very straightforward once you know how; the range of dates is controled by both the where clause and the connect by (number of levels). In the example above it will only return values for a month up to a maximum of 31 days. You could limit to return only the first half of a month by reducing the connect by level to 15, alternatively you can increase the range to any date range, such as a year by changing the to_date in the where clause. As you can see I'm passing the date variable into the query and using Oracle's date functions to determine the range for me because I feel this is a more user friendly experience for anyone else that has to run my scripts, but you could simply hard code the dates in.
Hope this helps,
James