如何在postgres中将整数分钟转换为间隔

时间:2022-06-30 02:51:58

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

我正在尝试将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

他们的任何功能是否会帮助我将其转换为间隔,或者我应该将其除以60并获得最终结果

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

2 个解决方案

#1


10  

Fastest way is with make_interval

最快的方法是使用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)

所以它看起来像这样(由@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

不是说这是最干净的,如果速度不是问题,我更喜欢*方法@a_horse_with_no_name提到

SELECT 20 * '1 minute'::interval;

#2


3  

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

或者:间隔'1'分钟* n如a_horse_with_no_name所示

#1


10  

Fastest way is with make_interval

最快的方法是使用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)

所以它看起来像这样(由@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

不是说这是最干净的,如果速度不是问题,我更喜欢*方法@a_horse_with_no_name提到

SELECT 20 * '1 minute'::interval;

#2


3  

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

或者:间隔'1'分钟* n如a_horse_with_no_name所示