修剪開頭和結尾不必要的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