If you want to remove decimals in the following Scenarios
SENARIO | ISAS | TOBE |
---|---|---|
1 | 17.0000000 | 17 |
2 | 20.0100 | 20.01 |
3 | 0 | 0 |
Then the following query should do the job.
SELECT F_Percent,
replace(rtrim(replace(convert(varchar, F_Percent),'0',' ')),' ','0') 'Trim Zeros',
CASE
WHEN replace(rtrim(replace(convert(varchar, F_Percent),'0',' ')),' ','0') IS NULL OR replace(rtrim(replace(convert(varchar, F_Percent),'0',' ')),' ','0') =''
OR replace(rtrim(replace(convert(varchar, F_Percent),'0',' ')),' ','0') ='0.'
THEN '0'
WHEN LEN(replace(rtrim(replace(convert(varchar, F_Percent),'0',' ')),' ','0')) =PATINDEX('%.%',replace(rtrim(replace(convert(varchar, F_Percent),'0',' ')),' ','0') )
THEN SUBSTRING(replace(rtrim(replace(convert(varchar, F_Percent),'0',' ')),' ','0'),0,LEN(replace(rtrim(replace(convert(varchar, F_Percent),'0',' ')),' ','0')))
ELSE replace(rtrim(replace(convert(varchar, F_Percent),'0',' ')),' ','0')
END 'Remove decimals',
PATINDEX('%.%',replace(rtrim(replace(convert(varchar, F_Percent),'0',' ')),' ','0') ) 'Decimal Location'
FROM (SELECT '3' F_Percent UNION SELECT '15' UNION SELECT '2.0' UNION SELECT '1000' UNION SELECT '20.01' UNION SELECT '0.10' UNION SELECT '1.5' UNION SELECT '20.5' UNION SELECT '0.07477'
UNION SELECT '0.11' UNION SELECT '1.0' UNION SELECT '0.0' UNION SELECT '0.0000' UNION SELECT '0.000850000' UNION SELECT '0.00510000' UNION SELECT '21.00000' UNION SELECT '0'
UNION SELECT '-1.5'
)TESTDATA
WHERE ISNUMERIC(F_Percent)=1
query execution results will look like the following Please check 'Remove decimals' column enter image description here