修剪开头和结尾不必要的0:
UPDATE yourTable
SET Prix = CASE
WHEN Prix LIKE '%.%[1-9]%' THEN -- has a non-0 after the decimal point
REPLACE(RTRIM(LTRIM(REPLACE(Prix, '0', ' '))), ' ', '0')
WHEN Prix LIKE '%.%' THEN -- only 0's after the decimal point, remove point
REPLACE(REPLACE(RTRIM(LTRIM(REPLACE(Prix, '0', ' '))), ' ', '0'), '.', '')
ELSE -- has no decimal point, only trim leading 0's
REPLACE(LTRIM(REPLACE(Prix, '0', ' ')), ' ', '0')
END
如果只想修剪尾随:
UPDATE yourTable
SET Prix = CASE
WHEN Prix LIKE '%.%[1-9]%' THEN -- has a non-0 after the decimal point
REPLACE(RTRIM(LTRIM(REPLACE(Prix, '0', ' '))), ' ', '0')
ELSE -- only 0's after the decimal point, remove point
REPLACE(REPLACE(RTRIM(LTRIM(REPLACE(Prix, '0', ' '))), ' ', '0'), '.', '')
END
WHERE Prix LIKE '%.%' -- make sure it's decimal
以上全部更换0
s的空间,使用内置的修剪功能修剪空格,然后再用0
s替换空格。
为什么你需要去3位小数? – 2013-02-25 17:17:46
'Prix'是'VARCHAR'字段吗?如果是这样,为什么? – Dukeling 2013-02-25 17:21:09
请不要要求SQL Server执行此操作 - 在表示层中对其所属的位置进行美化。 – 2013-02-25 17:21:38