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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Only two decimals (format a float)

Author  Topic 

henrikop
Constraint Violating Yak Guru

280 Posts

Posted - 2003-03-11 : 09:43:45
I have a query which returns

10674.54634
9565.49000
982
16562.456

I want my output to be with only (and always) two decimals, thus:

10674.55
9565.49
982.00
16562.46

English is not my native language and I can't find it in BOL nor this forum. I have to do this in SQL, because I have no influence on the front end application.

Thx, for the help!

Henri

~~~
SQL is nothing, writing it everything.

robvolk
Most Valuable Yak

15732 Posts

Posted - 2003-03-11 : 10:04:38
You can use CONVERT:

SELECT Convert(decimal(12,2), myCol) FROM myTable

CAST:

SELECT CAST(myCol AS decimal(12,2)) FROM myTable

Or the STR function:

SELECT LTRIM(Str(myCol,12,2)) FROM myTable

STR() will output a string, padded on the left with spaces. The LTRIM will remove the spaces if you want, otherwise you can remove the LTRIM and you'll have nicely formatted numbers aligned on the right side.

Go to Top of Page

henrikop
Constraint Violating Yak Guru

280 Posts

Posted - 2003-03-11 : 11:18:24
Thx for supplying 3 solutions! They have small differences. The third looks better, but in Holland we use , instead of . for numbers, and the third solution gives .'s which looks better, but gives weird outcomes in the webform.



Henri

~~~
SQL is nothing, writing it everything.
Go to Top of Page
   

- Advertisement -