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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43656783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:33:30  来源:igfitidea点击:

how to convert integer minutes to interval in postgres

sqlpostgresqlintervals

提问by Abhijeet Gulve

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 

回答by Evan Carroll

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;

回答by Vao Tsun

you can concat that integer with ' minutes':

您可以使用以下命令连接该整数' 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 * nas a_horse_with_no_name says

或者:interval '1' minute * n正如 a_horse_with_no_name 所说

回答by Edgar Ortega

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

如果您尝试在interval列中插入数据,您可以使用整数秒,PostgreSQL 会将其转换为间隔。