* previous version of addDays cause infinite loop

This commit is contained in:
Heng Sin Low 2007-01-26 14:59:56 +00:00
parent 73cffd61c0
commit c208946175
1 changed files with 7 additions and 2 deletions

View File

@ -22,13 +22,18 @@
CREATE OR REPLACE FUNCTION addDays(datetime TIMESTAMP WITH TIME ZONE, days Numeric) CREATE OR REPLACE FUNCTION addDays(datetime TIMESTAMP WITH TIME ZONE, days Numeric)
RETURNS DATE AS $$ RETURNS DATE AS $$
declare duration varchar;
BEGIN BEGIN
RETURN CAST(datetime AS DATE) + days; if datetime is null or days is null then
return null;
end if;
duration = days || ' day';
return cast(date_trunc('day',datetime) + cast(duration as interval) as date);
END; END;
$$ LANGUAGE plpgsql; $$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION subtractdays (day TIMESTAMP WITH TIME ZONE, days NUMERIC) CREATE OR REPLACE FUNCTION subtractdays (day TIMESTAMP WITH TIME ZONE, days NUMERIC)
RETURNS TIMESTAMP WITH TIME ZONE AS $$ RETURNS DATE AS $$
BEGIN BEGIN
RETURN addDays(day,(days * -1)); RETURN addDays(day,(days * -1));
END; END;