简体   繁体   English

如何在PostgreSQL中使用SQL记录分钟

[英]How to get minutes using SQL in PostgreSQL

I have a result using SQL inside a function 我在函数中使用SQL的结果

select a[2] as hour, a[3] minutes from ( select regexp_split_to_array('for  7h 18m', ' ') ) as dt(a);

and i got result as 我得到的结果是

hour    minutes
7h       18m

I want to get 我想得到

minutes
 438

该文档有一个使用摘录和纪元的示例

SELECT EXTRACT(EPOCH FROM '2h 18m'::INTERVAL)/60;

One option here is to remove the h from the hours string, multiply by 60 to get minutes, then add this quantity to the minutes field, with the m removed. 这里的一种选择是从小时字符串中删除h ,乘以60得到分钟,然后将此数量添加到分钟字段中,并删除m Try this query: 试试这个查询:

select regexp_replace(a[2], '[h\s]', '')::integer * 60 +
       regexp_replace(a[3], '[m\s]', '')::integer as minutes
from
(
    select regexp_split_to_array('for  7h 18m', ' ')
) as dt(a);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM