首页 > 解决方案 > 如何使用 Oracle regexp_replace 删除数字之间的空格和特殊字符

问题描述

我需要删除 Oracle 和 Netezza 文本字段中数字之间的所有空格和特殊字符。

Input:
[Any text 00 00 111 1   2222 ,?/!@#$ 33333 any text 123,. 45]. 

Output:
[Any text 0000111222233333 any text 123.45]

谢谢!

标签: sqlregexoracleregexp-replace

解决方案


您可以使用

SELECT REGEXP_REPLACE(col,'[^0-9]') AS new_col
  FROM tab

[:digit:]posix,例如

SELECT REGEXP_REPLACE(col,'[^[:digit:]]') AS new_col
  FROM tab

为了删除所有非数字字符,包括空格。

更新取决于您添加的请求

SELECT ID,
       col,       
       CASE
         WHEN REGEXP_INSTR(col, '[[:digit:]]') != 1 THEN
          REPLACE(REGEXP_SUBSTR(col, '[^[:digit:][:punct:]]+'), ' ')
       END || 
       REGEXP_REPLACE(col, '[^0-9]*([0-9]+|$)', '\1') ||
       REGEXP_SUBSTR(REGEXP_REPLACE(col, '[[:punct:] ]'), '[^[:digit:]]+$') AS new_col
  FROM tab

将删除从第一个数字到最后一个数字的任何非数字字符,并保持外部部分不变。

Demo


推荐阅读