Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL Server-从表中删除不必要的零_Sql_Sql Server 2008 - Fatal编程技术网

SQL Server-从表中删除不必要的零

SQL Server-从表中删除不必要的零,sql,sql-server-2008,Sql,Sql Server 2008,我想在显示此表时删除不必要的零 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

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

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
任何帮助都将不胜感激。
谢谢

修剪不需要的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位?
Prix
a
VARCHAR
字段吗?如果是,原因是什么?请不要要求SQL Server执行此操作-在表示层中对它所属的位置执行修饰。是的,Prix是此varchar字段中的varchar(30)。我希望在点(.)更新您的表集Prix=CASE,当Prix类似于“%.%”时,然后替换(RTRIM(LTRIM)(替换(Prix,'0',''),'','','0'))ELSE--没有小数点,只有修剪前导0的替换(LTRIM(REPLACE(Prix,'0',''),'0')END此解决方案工作正常,但如果我们有23651.0000,它会生成一个错误--它给出-->23651。而不是23651