create or replace function nice_interval( in dt interval ) returns interval as $$
declare
seconds int := cast ( extract( SECOND from dt ) as int );
days int := extract( DAY from dt );
months int := extract( MONTH from dt );
minutes int := extract( MINUTE from dt );
hours int := extract( HOUR from dt );
begin
if ( months = 0 ) then
if ( days = 0 ) then
if hours != 0 then
return hours * interval '1 hour';
elsif minutes != 0 then
return minutes * interval '1 minute';
elsif seconds = 0 then
return dt;
else
return seconds * interval '1 second';
end if;
else
return days * interval '1 day';
end if;
else
return months * interval '1 month';
end if;
end;
$$ language plpgsql;
Main Topics
Browse All Topics





by: earthman2Posted on 2007-06-03 at 02:24:36ID: 19203293
create or replace function nice_interval( in dt interval ) returns interval as $$
declare
seconds int := extract( SECOND from dt );
days int := extract( DAY from dt );
months int := extract( MONTH from dt );
minutes int := extract( MINUTE from dt );
hours int := extract( HOUR from dt );
begin
if ( months = 0 ) then
if ( days = 0 ) then
if hours != 0 then
return hours * interval '1 hour';
elsif minutes != 0 then
return minutes * interval '1 minute';
elsif seconds = 0 then
return dt;
else
return seconds * interval '1 second';
end if;
else
return days * interval '1 day';
end if;
else
return months * interval '1 month';
end if;
end;
$$ language plpgsql;