-2

I am trying to get a value rounded to two decimal places. However the results in the following code is still displaying 4 decimal places

select p.ProductName, p.UnitPrice, s.CompanyName, c.CategoryName,
 case 
    when c.CategoryName in ('Condiments', 'Beverages') then round((UnitPrice + (UnitPrice * .2)), 2)
    when c.CategoryName in ('meat/poultry', 'seafood') then round((UnitPrice + (UnitPrice * .15)), 2)
    when s.CompanyName = 'New Orleans Cajun Delights' then round((UnitPrice + (UnitPrice * .08)), 2)
    else p.unitprice
 end as NewUnitPrice
from products p
join Categories c on c.CategoryID = p.CategoryID
join Suppliers s on s.SupplierID = p.SupplierID

enter image description here

Dale K
  • 25,246
  • 15
  • 42
  • 71

1 Answers1

0

The solution is to change the data type to 2 points of precision. See the example below.

SELECT
    CAST(2.49532785 AS NUMERIC(32,8)) AS [Original]    --Original 8 precision numeric
    ,ROUND(CAST(2.49532785 AS NUMERIC(32,8)),2)  AS [Rounded]    --Rounded 8 precision numeric
    ,CAST(2.49532785 AS NUMERIC(32,2))  AS [Cast]    --Original 2 precision numeric
Andrew O'Brien
  • 1,793
  • 1
  • 12
  • 24
  • 3
    It would be better to mark this question as a duplicate instead of answering it. There are so many questions like this on SO. We don't need to answer yet another one.... – devlin carnate Nov 08 '21 at 20:55