简体   繁体   中英

How to remove trailing zeros from decimal number in SQL Server?

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?

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM