修剪前导和尾随不需要的 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,使用内置的trim函数修剪空格,然后0
再次用s替换空格。