Postgresql 9.1
I'm trying to use a query to find the difference, in fractional/decimal hours, between 2 dates and 2 times for a timesheet system. I'm using the query to make sure the software (not written by me) doesn't have any bugs. Here are the fields in the table I'm using: startdate is a Date field, starttime is a Time field, enddate is a Date field, endtime is a Time field.
I've looked at the date time docs for 9.1 and still haven't found what I need.
- age() takes 2 timestamps and appears to give a difference in integer, not fractional, days. I don't think it's possible to multiply the result of age() by 24 to get hours. Nor do I know how to include the time in the age() function.
- I could not find out to convert a date and time to some thing else to use another function.
- I have searched Google and Stackoverflow and have not found info to help me. I've been spending about 4 weeks on this off and on. So probably 30 hours already.
- NOTE: I don't think I can add user-defined functions. I don't have the permissions.
Example data:
- Startdate and time: '2016-04-29' and '23:00:00'
- Enddate and time: '2016-04-30' and '01:30:00'
I've also tried this sql statement.
SELECT employeetime.dcmasterid as empid,
nonchargeabletime.startdate as ncsdate,
nonchargeabletime.starttime as ncstime,
nonchargeabletime.enddate as ncedate,
nonchargeabletime.endtime as ncetime,
employeetime.dchours as normhrs,
(timestamp (startdate || ' ' || starttime) - timestamp (enddate || ' ' || endtime)) as diffhrs
FROM employeetime, nonchargeabletime
WHERE (nonchargeabletime.employeetime=employeetime.dcautoinc)
AND (nonchargeabletime.startdate >= '2016-04-24')
AND (nonchargeabletime.startdate <= '2016-04-30')
AND (employeetime.dcmasterid IN ('BLURG'))
AND (nonchargeabletime.nonchargeabletype=10)
ORDER BY employeetime.dcmasterid, nonchargeabletime.startdate, nonchargeabletime.starttime;
But I get a syntax error at startdate
where it says (timestamp (startdate ||
.
Anyone have any clues how to do this?
Thank you.
timestamp
column? – a_horse_with_no_name(z.startdate+z.starttime)::timestamp
will give you the combined values @a_horse_with_no_name : Ok! – wildplasser(enddate + endtime) - (startdate + starttime)
will return aninterval
– a_horse_with_no_name08:50:00
. How do I convert that to decimal hours? Is there a single function to convert that to decimal hours? – Bulrush