I need to trim trailing '0's from numbers such as 0.50, 0.40 etc. Any suggestions? Unable to use TRIM
function in SQL Server.
I don't want to use convert(decimal(10, 1), number)
since that will affect values such as '0.23' for ex.
I need to trim trailing '0's from numbers such as 0.50, 0.40 etc. Any suggestions? Unable to use TRIM
function in SQL Server.
I don't want to use convert(decimal(10, 1), number)
since that will affect values such as '0.23' for ex.
Here is a generic approach which should remove all trailing zeroes from any number. First, we cast the number to text, and count how many trailing zeroes are there, using REVERSE()
, REPLACE()
, and CHARINDEX()
. Then, we use LEFT()
to take the entire number text minus how many trailing zeroes were found.
SELECT LEFT(CAST(col AS VARCHAR),
LEN(CAST(col AS VARCHAR)) -
CHARINDEX(LEFT(REPLACE(REVERSE(CAST(col AS VARCHAR)), '0', ''), 1),
REVERSE(CAST(col AS VARCHAR))) +
CHARINDEX(LEFT(REPLACE(REVERSE(CAST(col AS VARCHAR)), '0', ''), 1),
REPLACE(REVERSE(CAST(col AS VARCHAR)), '0', ''))) AS new_col
FROM yourTable
Demo here:
U can try this script :
select convert (DOUBLE PRECISION , column_name) from table_name