Home about IT Motivation Course Sales Project About Me

Monday, June 11, 2012

script to create date range in MySQL


below quer to create date range in mysql.
usually used for create working calender for employee attendance
 
 
 
DROP PROCEDURE IF EXISTS datespopulate;

DELIMITER |

CREATE PROCEDURE datespopulate(dateStart DATE, dateEnd DATE)

BEGIN

  WHILE dateStart <= dateEnd DO

    INSERT INTO datetable (d) VALUES (dateStart);

    SET dateStart = date_add(dateStart, INTERVAL 1 DAY);

  END WHILE;

END;

|

DELIMITER ;

CALL datespopulate('2010-11-01','2015-01-01');
 
Note I named my table "datetable" and the column is named "d", but feel free to change this. Works fine on my end, let me know if you run in to an issue.

 source from : http://stackoverflow.com/questions/4736690/create-mysql-date-range

No comments: