【问题标题】:Oracle SQL display distinct none "standard alphanumeric caractersOracle SQL 显示不同的非\"标准字母数字字符
【发布时间】:2023-02-07 16:34:02
【问题描述】:

I need to find the way to list all the characters used in the column in order to narrow down the "Approved" values within the insert template we are creating... the idea is to allow all letters (only standard) without any dialect / country specific ones.

trying something like this... but need to have a list of the characters left over... like "$%()* etc.

SELECT * FROM mytable WHERE REGEXP_LIKE(column_1,^[a-zA-Z0-9-]+$)
  • I googled "oracle find special characters in string" and I really got a lot of hits. Did you go through those ? What didn't work ? Can you provided a bit of sample data with the expected behaviour ?

标签: sql regex oracle


【解决方案1】:

要找到其他字符,您可以删除您期望的字符,然后查看还剩下什么:

SELECT REGEXP_REPLACE( column1, '[a-zA-Z0-9-]' ) AS other_characters
FROM   mytable
WHERE  REGEXP_REPLACE( column1, '[a-zA-Z0-9-]' ) IS NOT NULL

如果要连接和删除重复字符:

WITH replace_expected ( str ) AS (
  SELECT REGEXP_REPLACE( column1, '[a-zA-Z0-9-]' )
  FROM   mytable
  WHERE  REGEXP_REPLACE( column1, '[a-zA-Z0-9-]' ) IS NOT NULL
),
split_strings ( str, pos, ch ) AS (
  SELECT str, 1, SUBSTR(str, 1, 1)
  FROM   replace_expected
UNION ALL
  SELECT str, pos + 1, SUBSTR(str, pos + 1, 1)
  FROM   split_strings
  WHERE  pos < LENGTH(str)
)
SELECT LISTAGG(DISTINCT ch) WITHIN GROUP (ORDER BY ch) AS other_characters
FROM   split_strings;

fiddle

【讨论】:

    【解决方案2】:

    两个步骤:

    1. 从列中的所有字符串中提取特殊字符并将它们连接成一个长字符串(可能有许多字符在字符串中多次出现)。
    2. 使用递归查询遍历字符并返回具有不同字符的字符串。

      查询:

      with one_row (str) as
      (
        select listagg(regexp_replace(column_1, '[a-zA-Z0-9-]'))
        from mytable
        where regexp_like(column_1, '[^a-zA-Z0-9-]')
      )
      select listagg(distinct substr(str, level, 1)) as c
      from one_row
      connect by level <= length(str);
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-15
      • 1970-01-01
      • 2015-11-01
      • 2018-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多