oracle 检查所有字符是否为“X”

rn0zuynd  于 2023-02-03  发布在  Oracle
关注(0)|答案(4)|浏览(125)

我有下表:

COL
---
XXY
YXX
XXX
NULL

我想过滤掉不全是“X”的行。预期输出:

COL
---
XXX
pvcm50d1

pvcm50d11#

我们可以在这里使用REGEXP_LIKE

SELECT COL
FROM yourTable
WHERE REGEXP_LIKE(COL, '^X+$');  -- ^X+$ means all X from start to end

另一个类似的版本:

SELECT COL
FROM yourTable
WHERE NOT REGEXP_LIKE(COL, '[^X]');  -- this means no non X present
nfeuvbwi

nfeuvbwi2#

另一个选项(不使用 * 正则表达式 *)可能是使用

WITH t(col) AS
(
 SELECT 'XXY' FROM dual UNION ALL
 SELECT 'YXX' FROM dual UNION ALL
 SELECT 'XXX' FROM dual UNION ALL
 SELECT  NULL FROM dual UNION ALL
 SELECT 'XX ' FROM dual 
)
SELECT *
  FROM t
 WHERE REPLACE(NVL(col,'Y'),'X') IS NULL;

COL
----
XXX

通过使用NVL()而不忘记col = NULL的情况

g52tjvyc

g52tjvyc3#

您可以使用以下语法(假设您使用的是MySQL数据库5.6或更高版本):

SELECT * FROM table_name WHERE col_name REGEXP '^X+$';
u1ehiz5o

u1ehiz5o4#

如果您不想要/没有regexp,则:

WITH
    tbl AS
        (   Select 'XXY' "COL" From dual Union All
            Select 'YXX' "COL" From dual Union All
            Select 'XXX' "COL" From dual Union All
            Select null "COL" From dual 
        )
Select  COL 
From    tbl
Where   Length(Nvl(COL, 'Z')) - Length( Replace(  Upper(Nvl(COL, 'Z')), 'X', '')) Is Null

COL
---
XXX

如果需要的话,它同时包含小写'x'和大写'X',并返回原始COL值

相关问题