how to convert integer minutes to interval in postgres

左心房为你撑大大i 提交于 2020-12-01 08:24:53

问题


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 

回答1:


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;



回答2:


you can concat that integer with ' minutes':

t=# with i as (
  select 20::int n
)
select concat(n,' minutes')::interval 
from i;
  concat
----------
 00:20:00
(1 row)

Time: 1.220 ms

Update:

Or: interval '1' minute * n as a_horse_with_no_name says




回答3:


In the case you are trying to insert data in an interval column, you can use integer seconds and PostgreSQL will convert it to a interval.



来源:https://stackoverflow.com/questions/43656783/how-to-convert-integer-minutes-to-interval-in-postgres

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!