0

我想在显示此表时删除不必要的零

Produit | Prix
---------------
   A    | 1000.65400
   B    | 1200.654000
   C    | 2540.6540000
   D    | 1000.25000
   E    | 224000.6540000000

我想得到这个结果:

Produit | Prix
---------------
   A    | 1000.654
   B    | 1200.654
   C    | 2540.654
   D    | 1000.25
   E    | 224000.654

任何帮助将不胜感激。谢谢

4

1 回答 1

0

修剪前导和尾随不需要的 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

上面用空格替换所有0s,使用内置的trim函数修剪空格,然后0再次用s替换空格。

于 2013-02-25T17:26:39.903 回答