August 11, 2016 at 3:27 pm
The rounding works fine to two decimal places if I try to convert directly but when I put in a sub query then it doesn't do anything.
select CAST(391/60.00 as decimal(38, 2))
returns 6.52
but the query below gives me 6.51999998092651 and it is not rounding anything.
SELECT pp_id AS pay_period_id,
employee_no,
reg,
ovt
FROM (SELECT pp.id AS pp_id,
pr.employee_no,
CASE WHEN E.pay_type = 4 then pr.hours else CAST(391/60.00 as decimal(38,2)) END AS [hours],
rate_type
FROM pay_records AS pr
INNER JOIN pay_period pp ON pr.[date] BETWEEN pp.start_period AND pp.end_period AND valid_payroll = 0
INNER JOIN employee E ON E.Employee_no=pr.employee_no) AS p
PIVOT (Sum([hours])
FOR rate_type IN ([REG],
[OVT])) AS pv
August 11, 2016 at 3:30 pm
shezi (8/11/2016)
The rounding works fine to two decimal places if I try to convert directly but when I put in a sub query then it doesn't do anything.
select CAST(391/60.00 as decimal(38, 2))
returns 6.52
but the query below gives me 6.51999998092651 and it is not rounding anything.
SELECT pp_id AS pay_period_id,
employee_no,
reg,
ovt
FROM (SELECT pp.id AS pp_id,
pr.employee_no,
CASE WHEN E.pay_type = 4 then pr.hours else CAST(391/60.00 as decimal(38,2)) END AS [hours],
rate_type
FROM pay_records AS pr
INNER JOIN pay_period pp ON pr.[date] BETWEEN pp.start_period AND pp.end_period AND valid_payroll = 0
INNER JOIN employee E ON E.Employee_no=pr.employee_no) AS p
PIVOT (Sum([hours])
FOR rate_type IN ([REG],
[OVT])) AS pv
Before I pursue an answer I have a question. What is the data type for this column, pr.hours, in the above query?
August 11, 2016 at 3:43 pm
pr.Hours is real and where you see the hardcoded value that is also column(ActualHours) which is integer but I hardcoded the value though.
August 12, 2016 at 9:17 am
shezi (8/11/2016)
pr.Hours is real and where you see the hardcoded value that is also column(ActualHours) which is integer but I hardcoded the value though.
With a simple setup I can't get what you do. What I would suggest is to use the round function instead of casting as decimal(38,2) value.
August 12, 2016 at 9:24 am
In your CASE statement, try changing "pr.hours" to "CAST(pr.hours as decimal(38,2))".
For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]
August 12, 2016 at 9:27 am
Alvin Ramard (8/12/2016)
In your CASE statement, try changing "pr.hours" to "CAST(pr.hours as decimal(38,2))".
You are going down the path I was, just couldn't recreate the problem with a simple setup.
August 12, 2016 at 9:33 am
that was the issue. I also had to cast or round the "pr.hours" field as well otherwise it wouldn't round the hard coded value i had in there. Thanks Guys!
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply