1

In my attendance table, the Overtime is mentioned in the format 'hh:mm'. Its String column in the database. Full month Overtime is entering as a single entry. So my column data is as follows

OVERTIME
'52:00'
'30:00'
'98:00'
'06:00'

Now how can get the SUM of the OverTime column.

I have tried SELECT SUM(OVERTIME::TIME) FROM ATTENDANCE. Its giving me the error :

ERROR: date/time field value out of range: "52:00"

4
  • 2
    time can only be used for times of the day (max. 24 hours). Try with interval instead. Commented Jul 23, 2018 at 13:00
  • @stickybit. I'm new with INTERVAL, Can you give an example to use Interval in this scenario, pls. Commented Jul 23, 2018 at 13:05
  • Just replace ::time with ::interval to cast to an interval rather than to a time. And consider changing the column's type to interval if possible. Commented Jul 23, 2018 at 13:08
  • @stickybit, Thanks, Let me try Commented Jul 23, 2018 at 13:13

2 Answers 2

1

Assuming the values are HH:MM, then you can use string manipulations:

select sum(left(overtime, 2)::numeric + right(overtime, 2)::numeric/60) as hours

Note that this will give the results as decimal hours.

If you want the values as an interval, you can also do that:

select sum( cast(overtime as interval) )

Note that this will give the results as days, hours, and minutes.

Sign up to request clarification or add additional context in comments.

Comments

0

The other solution didn't it in my case because I have a mix of formats i-e H:MM or HH:MM. This is what worked for me:

select sum(SPLIT_PART(overtime,':',1)::numeric + split_part(overtime,':',1)::numeric/60) as hours from your_table

Comments

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.