I had a similar problem, but also needed to remove a decimal point that did not have a decimal, here is my solution, which breaks the decimal into its components and based on the number of characters it takes from the decimal point in the string, the length of the fraction component (without using CASE). To make it even more interesting, my number was stored as a float without decimals.
DECLARE @MyNum FLOAT SET @MyNum = 700000 SELECT CAST(PARSENAME(CONVERT(NUMERIC(15,2),@MyNum/10000),2) AS VARCHAR(10)) + SUBSTRING('.',1,LEN(REPLACE(RTRIM(REPLACE(CAST(PARSENAME(CONVERT(NUMERIC(15,2),@MyNum/10000),1) AS VARCHAR(2)),'0',' ')),' ','0'))) + REPLACE(RTRIM(REPLACE(CAST(PARSENAME(CONVERT(NUMERIC(15,2),@MyNum/10000),1) AS VARCHAR(2)),'0',' ')),' ','0')
The result is painful, I know, but I got there, with a lot of help from the answers above.
Adge Cutler Jun 03 '14 at 9:30 a.m. 2014-06-03 09:30
source share