Tell me more ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I need to generate a list of dates with MySQL between two given dates. This list of dates must be filtered to the dates that correspond to Tuesday and Thursday. Is it possible?

share|improve this question
Did my answer ever work for you ??? – RolandoMySQLDBA Mar 28 '12 at 19:03
Did my answer ever work for you ??? – RolandoMySQLDBA Apr 16 at 11:10

2 Answers

A really easy way to do it is to have a table of numbers. Query this table for numbers less than the number of days between the two dates, and select the smaller date plus the number to return the dates themselves. Easy to stick a predicate in the where clause to handle Tuesdays and Thursdays too.

share|improve this answer

Here is a stored function called MakeDateList:

DELIMITER $$
DROP FUNCTION IF EXISTS `junk`.`MakeDateList` $$
CREATE FUNCTION `junk`.`MakeDateList`
(
  BeginDate DATE,
  EndDate DATE,
  InclusionList VARCHAR(20)
)
RETURNS VARCHAR(4096)
DETERMINISTIC
BEGIN
  DECLARE RunningDate DATE;
  DECLARE rv VARCHAR(4096);
  DECLARE comma CHAR(1);
  DECLARE IncList,DOWValue VARCHAR(20);
  DECLARE OK_To_Add INT;

  SET IncList = CONCAT(',',InclusionList,',');
  SET comma = '';
  SET rv = '';
  SET RunningDate = BeginDate;
  WHILE RunningDate <= EndDate DO
    SET OK_To_Add = 0;
    SET DOWValue = CONCAT(',',DAYOFWEEK(RunningDate),',');
    IF LOCATE(DOWValue,IncList) > 0 THEN
      SET OK_To_Add = 1;
    END IF;
    IF OK_To_Add = 1 THEN
      SET rv = CONCAT(rv,comma,RunningDate);
    END IF;
    SET comma = ',';
    SET RunningDate = RunningDate + INTERVAL 1 DAY;
  END WHILE;
  RETURN rv;
END $$
DELIMITER ;

It has three parameters: BeginDate, EndDate, and InclusionList

InclusionList is a comma speararted list of numbers (valid numbers 1-7)

  • 1 = Sunday
  • 2 = Monday
  • 3 = Tuesday
  • 4 = Wednesday
  • 5 = Thursday
  • 6 = Friday
  • 7 = Saturday

So to get Tuesdays and Thursdays, make the InclusionList 3,5

Example : Calling MakeDateList('2011-10-01','2011-10-01','3,5') should give you Tuesdays and Thursdays for the whole month of October 2011

Here is the loading of the stored function:

mysql> DELIMITER $$
mysql> DROP FUNCTION IF EXISTS `junk`.`MakeDateList` $$
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE FUNCTION `junk`.`MakeDateList`
    -> (
    ->   BeginDate DATE,
    ->   EndDate DATE,
    ->   InclusionList VARCHAR(20)
    -> )
    -> RETURNS VARCHAR(4096)
    -> DETERMINISTIC
    -> BEGIN
    ->   DECLARE RunningDate DATE;
    ->   DECLARE rv VARCHAR(4096);
    ->   DECLARE comma CHAR(1);
    ->   DECLARE IncList,DOWValue VARCHAR(20);
    ->   DECLARE OK_To_Add INT;
    ->
    ->   SET IncList = CONCAT(',',InclusionList,',');
    ->   SET comma = '';
    ->   SET rv = '';
    ->   SET RunningDate = BeginDate;
    ->   WHILE RunningDate <= EndDate DO
    ->     SET OK_To_Add = 0;
    ->     SET DOWValue = CONCAT(',',DAYOFWEEK(RunningDate),',');
    ->     IF LOCATE(DOWValue,IncList) > 0 THEN
    ->       SET OK_To_Add = 1;
    ->     END IF;
    ->     IF OK_To_Add = 1 THEN
    ->       SET rv = CONCAT(rv,comma,RunningDate);
    ->       SET comma = ',';
    ->     END IF;
    ->     SET RunningDate = RunningDate + INTERVAL 1 DAY;
    ->   END WHILE;
    ->   RETURN rv;
    -> END $$
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER ;

Here is the result of the example:

mysql> SELECT MakeDateList('2011-10-01','2011-10-31','3,5');
+-----------------------------------------------------------------------------------------+
| MakeDateList('2011-10-01','2011-10-31','3,5')                                           |
+-----------------------------------------------------------------------------------------+
| 2011-10-04,2011-10-06,2011-10-11,2011-10-13,2011-10-18,2011-10-20,2011-10-25,2011-10-27 |
+-----------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql>

Give it a Try !!!

share|improve this answer
Thanks, I will try. – cabita Nov 21 '11 at 3:18
1  
@cabita : Did this work for you ??? – RolandoMySQLDBA Nov 28 '11 at 19:09
@cabita : Did this work for you ??? – RolandoMySQLDBA Apr 16 at 11:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.