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
 SQL Server Development (2000)
 Format numbers

Author  Topic 

Hommer
Aged Yak Warrior

808 Posts

Posted - 2006-09-06 : 11:27:27
This one I am sure is easy to some of you, but I had hard time to find a good way to get around.

I have a set of data in the type of varchar nullable, and they look like Null, 2.62500, 3.952800, 5.000000, and 10.000000.

And the end results should be:
(empty string for the null), 2.625, 3.9528, 5, and 10

What are the elegant ways to achieve this? I did not find any string function that will help. I am afraid it has to be converted to decimal(?) then round(?) function then convert back?

I am stuck with data type.

Thanks!

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-09-06 : 11:44:29
Use the FLOAT, Hummer.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-09-06 : 12:09:58
If you want to store numbers then why do you use varchar datatype? Use proper decimal or float datatype
Also if you use front end application, use format function there

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-09-06 : 12:20:48
[code]declare @test table(n varchar(20))

insert @test
select Null union all
select '2.62500' union all
select '3.952800' union all
select '5.000000' union all
select '10.000000'

select n,
CAST(CAST(n AS FLOAT) AS VARCHAR)
from @test[/code]

Peter Larsson
Helsingborg, Sweden
Go to Top of Page

Hommer
Aged Yak Warrior

808 Posts

Posted - 2006-09-06 : 12:24:50
Thanks Peso and Madhivanan!

I know the normal rules, i.e. let the front end do the formating and stored the data in the right data type.

Unfortunately, I have no control over either one.

I will convert them into float and see what I get.
Go to Top of Page
   

- Advertisement -