tags:

views:

296

answers:

3

Hi,

I want to ask if anybody know the query to drop the 0 value in decimal..

E.g : A field name percent have these values

Percent

770.00000000000000000000, 340.670000000000000000000, 96.00000000000000000000, 4400.56000000000000000000, 109.89000000000000000000, 109.00000000000000000000, 37.00000000000000000000,

Currently I'm using the query "select cast([Percent] as decimal(9,2)) as [Percent] from table" and will result in

Result

770.00, 340.67, 96.00, 4400.56, 109.89, 109.00, 37.00,

I want the result this actually:->

770, 340.67, 96, 4400.56, 109.89, 109, 37,

+1  A: 

You could rather just cast to FLOAT.

astander
Then you can end up with values as 340.69999999999999 however
Peter
+1 for FLOAT suggestion though i also agree with Johannes Rössel comments in the original post
kevchadders
That is true, you could end up with a FLOATING point number misrepresentation. Other than that I am not aware of any way of doing this in Sql
astander
A: 

You could use a combination of DECIMAL and FLOAT. Decimal first to round it down to 2 deciaml places, then float to remove unwanted 0's

e.g.

select cast(cast([Percent] as decimal(9,2)) AS FLOAT) as [Percent]

With the example of 340.69999999999999, first it round to 340.70, then it takes away the zero giving you 340.7. As with any rounding some precision will be lost.

kevchadders
Not correct i think : eg select cast(cast (0.55 as decimal(10,2)) as float) as pyields : 0.55000000000000004
Peter
Peter, I ran that SQL in your example in Management Studio (SQL Server) and still got 0.55?
kevchadders
I ran it in sql query analyzer sql 2k, I try to find an example in MStudio
Peter
Intersting. I also tried it in SQL Query Analyzer 2000 and got 0.55000000000000004, but SQL Server 2005 and SQl Server 2008 both return it as 0.55
kevchadders
But the point is that converting to float, what you do last can always yield strange results since float is not capable to represent all decimal values correct
Peter
It has to do with how the floats are stored, tip : make it a SO question, you have my vote :-)
Peter
Created the question here Peter ;) http://stackoverflow.com/questions/1831827/how-floats-are-stored-in-sql-server-2000-2005-2008
kevchadders
hanks guys!!! I agree with Johannes and Peter but at the same time kevchadders query resulted to my expectation....in that case I'm gona need to ask the end users which one do they want....
rathu
+1  A: 

This rather nasty TSQL might just do the job :

select 
  case 
    right(
       cast(cast([percent] as decimal(9,2)) as nvarchar(11))
    ,2)

    when '00' then cast(cast([percent] as int) as nvarchar(11)) as [percent]
    else cast(cast([percent] as decimal(9,2)) as nvarchar(11)) as [percent]

 end
from table

of course it is always returning a string, but that's inherent to your demands, you are looking for a representation for a value...

I think you should postpone that representation to where it makes more sense (report, datagrid?) and you have more tools (like string.format kinda tools) to do the job better.

Peter