0

How can I remove trailinging zeros from decimal number in SQL Server? If I have as percentage field in database with value as 96.86, my query should return 96.86, but if the value is 96.00 then i need to fetch only 96. How can this be done?

Taufik Shaikh
  • 259
  • 1
  • 3
  • 9

2 Answers2

1

i think you should use

select cast(CAST(25.00 as decimal(18,5)) as float)

it return 25 but you cast

   select cast(CAST(25.23 as decimal(18,5)) as float)

it return 25.23

Manish Singh
  • 934
  • 1
  • 12
  • 27
0

You can convert to a string and then trim the trailing 0 characters. Unfortunately, SQL Server does not have really powerful string functions. The following does do what you want:

select (case when nn like '%.' then replace(nn, '.', '') else nn end)
from (select 96.86 as n union all select 96) t cross apply
     (values (ltrim(rtrim(replace(cast(n as varchar(255)), '0', ' '))))) v(nn)
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786