1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| drop database if exists common;
create database common default character set utf8 collate utf8_general_ci;
use common;
delimiter |
create function periodes(debut datetime,fin datetime) returns text deterministic
begin
declare duree int(4) default datediff(fin,debut);
declare retour text;
declare t char(7);
declare e int(4);
while debut<fin do
set e=datediff(last_day(debut),debut)+1;
set t=left(debut,7);
set debut=date_add(debut, interval e day);
set duree=duree-e;
set retour=concat_ws("#",retour,concat(t,";",if(debut<fin,e,duree+e+1)));
end while;
return retour;
end|
delimiter ;
select common.periodes("2010-01-14","2010-02-07"); |
Partager