====== Fonctions utiles pour les dates ======
===== Fonction de vérification de l'existence d'une date =====
CREATE OR REPLACE FUNCTION cenlr_date_est_valide(integer, integer, integer)
RETURNS boolean AS
$BODY$
DECLARE
jour alias for $1;
mois alias for $2;
annee alias for $3;
var_result boolean;
BEGIN
IF (mois IN (1,3,5,7,8,10,12) AND jour <=31) THEN
var_result = true;
RETURN var_result;
ELSIF (mois IN (4,6,9,11) AND jour <=30) THEN
var_result = true;
RETURN var_result;
ELSIF ((mois = 2 AND (annee % 4) = 0 AND jour <=29) OR (mois = 2 AND (annee % 4) <> 0 AND jour <=28)) THEN
var_result = true;
RETURN var_result;
ELSE var_result = false;
RETURN var_result;
END IF;
RETURN null;
END;
$BODY$
LANGUAGE 'plpgsql' VOLATILE
COST 100;
===== Fonction de nommage du jour passé en paramètre =====
CREATE OR REPLACE FUNCTION day_name(date)
RETURNS text AS
$BODY$
DECLARE
date_test alias for $1;
var_result text;
myrec RECORD;
BEGIN
FOR myrec IN
SELECT CASE extract(isodow from date_test)
WHEN 1 then 'lundi'
WHEN 2 THEN 'mardi'
WHEN 3 THEN 'mercredi'
WHEN 4 THEN 'jeudi'
WHEN 5 THEN 'vendredi'
WHEN 6 THEN 'samedi'
WHEN 7 THEN 'dimanche'
END as jour
LOOP
var_result:=myrec.jour;
END LOOP;
RETURN var_result;
END;
$BODY$
LANGUAGE 'plpgsql' VOLATILE
COST 100;
$> select day_name('2010-02-07');
$> dimanche