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?
vbnetcoderAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Steve WalesSenior Database AdministratorCommented:
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)
dsackerContract ERP Admin/ConsultantCommented:
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
Kyle AbrahamsSenior .Net DeveloperCommented:
select CAST (SUM(Field1 * 100) / field2 as numeric(18,2))

cast automatically rounds.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

vbnetcoderAuthor Commented:
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):"
Steve WalesSenior Database AdministratorCommented:
I think he means if you have 151.00. Do you want it to display as 151.00 or 151.
dsackerContract ERP Admin/ConsultantCommented:
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. :)
vbnetcoderAuthor Commented:
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...
Steve WalesSenior Database AdministratorCommented:
My response and Kyle's both give you that syntax.  If it is not work, please post the error you are getting
vbnetcoderAuthor Commented:
ty
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.