Postgres - Convert elapse time to HH:MM:SS format

Viewed 111

I wanted to calculate elapsed time in format 20:59:59, I have start and end time for my particular record.

Currently, I used to subtract, end date time with start date time to calculate the elapsed time but I am getting the output into this format.

Can we change this to HH:MM:SS format(in this case: 02:23:05).

0 years 0 mons 0 days 2 hours 23 mins 5.0 secs
1 Answers

You are getting a type interval. You can directly cast this into type time:

demo:db<>fiddle

SELECT myresult::time

If you want to get string output, you can use datetime formatting using to_char and the 'HH24:MI:SS' pattern:

demo:db<>fiddle

SELECT to_char(my_result, 'HH24:MI:SS')
Related