how to convert integer minutes to interval in postgres

Abhijeet Gulve picture Abhijeet Gulve · Apr 27, 2017 · Viewed 18.9k times · Source

I'm trying to convert minutes which are in integer to interval in postgres

Is their any function that will help me to convert it to interval or should i have divide it by 60 and get the final result

20 minutes will be like 00:20:00 as result 

Answer

Evan Carroll picture Evan Carroll · Apr 27, 2017

Fastest way is with make_interval

make_interval(years int DEFAULT 0, months int DEFAULT 0, weeks int DEFAULT 0, days int DEFAULT 0, hours int DEFAULT 0, mins int DEFAULT 0, secs double precision DEFAULT 0.0)

So it looks like this (as suggested by @Teddy)

SELECT make_interval(mins => 20);

or,

SELECT make_interval(0,0,0,0,0,20);

Not to say that's the cleanest, if speed isn't an issue I prefer the * method @a_horse_with_no_name mentioned

SELECT 20 * '1 minute'::interval;