1

I have a result using SQL inside a function

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

2 Answers 2

4

The documentation has an example using extract and epoch.

SELECT EXTRACT(EPOCH FROM '2h 18m'::INTERVAL)/60;
Sign up to request clarification or add additional context in comments.

1 Comment

This is also awesome...Thanks
2

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. 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);

1 Comment

getting error Error in query: ERROR: invalid input syntax for integer: ""

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.