Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Cutting down decimal places in returned result

Author  Topic 

Grifter
Constraint Violating Yak Guru

274 Posts

Posted - 2010-07-01 : 12:03:55
Hi

In one of my select fields in a query I have the following:

CAST(dbo.FunctionA 'Param1') AS FLOAT)/CAST((dbo.FunctionB, 'ParamB') AS FLOAT) AS Alias

My returned value is like this:

1.23233424333

But I want to have it only with 2 decimal places but am unsure what to use e.g. Cast whole result to decimal or what and can't really find a suitable example that reflects my problem.

Could anyone suggest what it is I should do, I know there are probably 2 or 3 options I could use.

G

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2010-07-01 : 12:09:57
There a several methods, the most common seem to be CASTing as DECIMAL/NUMERIC or using the ROUND function.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-07-01 : 12:25:51
WHY are you using FLOAT?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-07-01 : 12:27:36
CONVERT(varchar(20), MyMoneyDatatype, 1)

will do it ... got to be able to CAST the value as MONEY though

Should really do the "presentation" stuff in the front end though.
Go to Top of Page

Grifter
Constraint Violating Yak Guru

274 Posts

Posted - 2010-07-02 : 05:55:09
quote:
WHY are you using FLOAT?

Brett


One of the functions returns an INT and the other returns float. I suppose now I should only be casting one to float but i think originally it was to make sure I was dividing two of the same data types.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-07-02 : 08:41:40
round(your_value_or_expression,2,1) as YourName
will cut the value to 2 decimals instead of rounding the value.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -