MySQL Forums
Forum List  »  Newbie

Re: select dates in a month that fall on a given day of the week
Posted by: Peter Brawley
Date: May 28, 2009 10:15AM

> was hoping I could do it without building a table of dates first.

SQL isn't meant to be a complete computer language; it's designed to depend on relational tables.

Building a calendar table is dead simple. The result is useful for lots of tasks:
DROP TABLE IF EXISTS digits,calendar;
CREATE TABLE digits(i INT PRIMARY KEY);
INSERT INTO digits VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
DROP TABLE IF EXISTS calendar;
CREATE TABLE calendar
SELECT CURDATE() + INTERVAL t.i*10 + u.i DAY AS Date
FROM digits AS t 
JOIN digits AS u
JOIN digits AS v
WHERE ( t.i*100 + u.i*10 + v.i ) < 1000;

PB
http://www.artfulsoftware.com

Options: ReplyQuote


Subject
Written By
Posted
Re: select dates in a month that fall on a given day of the week
May 28, 2009 10:15AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.