Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert integer minutes to interval in postgres

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  
like image 839
Abhijeet Gulve Avatar asked Apr 27 '17 11:04

Abhijeet Gulve


People also ask

How do I create an interval in PostgreSQL?

In PostgreSQL, the make_interval() function creates an interval from years, months, weeks, days, hours, minutes and seconds fields. You provide the years, months, weeks, days, hours, minutes and/or seconds fields, and it will return an interval in the interval data type.

What is interval data type in PostgreSQL?

In PostgreSQL the interval data type is used to store and manipulate a time period. It holds 16 bytes of space and ranging from -178, 000, 000 years to 178, 000, 000 years. It also has additional attribute called “precision (denoted by p)” that can be used to set the level of precision in the query results.

How interval works in PostgreSQL?

In PostgreSQL, the Interval is another type of data type used to store and deploy Time in years, months, days, hours, minutes, seconds, etc. And the months and days values are integers values, whereas the second's field can be the fractions values.

Is int and Integer same in PostgreSQL?

There are three kinds of integers in PostgreSQL: Small integer ( SMALLINT ) is 2-byte signed integer that has a range from -32,768 to 32,767. Integer ( INT ) is a 4-byte integer that has a range from -2,147,483,648 to 2,147,483,647.


1 Answers

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; 
like image 58
NO WAR WITH RUSSIA Avatar answered Sep 23 '22 15:09

NO WAR WITH RUSSIA