简体   繁体   中英

SQL ROUND() function issue

I have tried to use round function to convert for example 348.426580 to 348.43
But in below query I used the result I got is 348.430000 in [ShippingCost] column

How can I omit the four zeros?

SELECT  S.Product_Name, 
        SPD.UnitPrice, 
        SPD.Quantity, 
        SPD.Quantity * SPD.UnitPrice Amount,
        CONVERT(INT,(SPD.Quantity * SPD.UnitPrice)/(SUM(SPD.Quantity * SPD.UnitPrice) OVER()) * 100) [Cost %],
        ROUND((SPD.Quantity * SPD.UnitPrice)/(SUM(SPD.Quantity * SPD.UnitPrice) OVER()) * Sp.Shipping_Cost,2) [ShippingCost],
        (SPD.Quantity * SPD.UnitPrice)/(SUM(SPD.Quantity * SPD.UnitPrice) OVER()) * Sp.Customs_Cost [CustomsCost],
        (SPD.Quantity * SPD.UnitPrice)/(SUM(SPD.Quantity * SPD.UnitPrice) OVER()) * Sp.Shipping_Cost + (SPD.Quantity * SPD.UnitPrice)/(SUM(SPD.Quantity * SPD.UnitPrice) OVER()) * Sp.Customs_Cost +SPD.Quantity * SPD.UnitPrice - SpD.Discount [TotalAmount]
FROM dbo.Stock_Purchase SP
INNER JOIN dbo.Stock_Purchase_Details SPD
    ON SP.Purchase_ID = SPD.Purchase_ID 
INNER JOIN dbo.Store S
    ON SPD.Pro_ID = S.Pro_ID;

You can cast to a decimal, say DECIMAL(10, 2) . Because casting automatically does rounding, there is no need for round() :

    CAST((SPD.Quantity * SPD.UnitPrice)/(SUM(SPD.Quantity * SPD.UnitPrice) OVER()) * Sp.Shipping_Cost as decimal(10, 2)) as [ShippingCost],
ROUND(column_name,decimals)

column_name Required for The field to round.

decimals Required for Specifies the number of decimals to be returned.

See this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM