Link to home
Start Free TrialLog in
Avatar of vbnetcoder
vbnetcoder

asked on

SQL - truncating to two decimal places

This query produces stuff like

SELECT
      Round( (SUM(Field1 * 100) / Field2, 2) AS MktPct
FROM Table

12.2300000
1.340000

How would i get rid of the extra zeros after the decimal point?
Avatar of Steve Wales
Steve Wales
Flag of United States of America image

How about:

SELECT
      CAST(Round( (SUM(Field1 * 100) / Field2, 2) as decimal(18,2)) AS MktPct
FROM Table

(Adjust the 18 to fit how many significant digits you need)
Assuming you wouldn't want any non-needed zero (even if it's even in the first or second places after the decimal):

SELECT
      CONVERT(float, Round( (SUM(Field1 * 100) / Field2, 2)) AS MktPct
FROM Table
ASKER CERTIFIED SOLUTION
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vbnetcoder
vbnetcoder

ASKER

I think that is right.

I don't understand the following quote .. could you give me an example of what could go wrong?

"Assuming you wouldn't want any non-needed zero (even if it's even in the first or second places after the decimal):"
I think he means if you have 151.00. Do you want it to display as 151.00 or 151.
Actually, I wrote that before re-reading your heading. You want two decimal places, and I answered as if you would want things like 1.1000000 to look like 1.1 rather than 1.10. The other guys have given you better solutions if you want the cents (2 decimal places) to show. They deserve the points.

You can save the use of FLOAT for something else in the future. :)
this is working with the  CONVERT(float,Round( (SUM(t1.[Market Value]) * 100) / TotMktValue, 2, 2)) AS MktPct


How would i change it to use cast ... i keep messing up the syntax...
My response and Kyle's both give you that syntax.  If it is not work, please post the error you are getting
ty