首页 > 解决方案 > 按模式返回具有逗号分隔值的行 (Oracle SQL)

问题描述

我有一些带有逗号分隔值的字段:

1,2,AA,4,Z
1,ZZ,44,A,1
1,44,33,AA,Z
4,2,Z,A,F
1.2,4,E,1,1
F,1,3.4,3,A

是否可以使用规则选择行:数字、数字、字符、任何值?

结果可以是:

1,2,AA,4,Z
4,2,Z,A,F
1.2,4,E,1,1

我正在尝试使用REGEXP_LIKE

with s as (
select '1,2,AA,4,Z'   val from dual
union all
select '1,ZZ,44,A,1'  val from dual
union all
select '1,44,33,AA,Z' val from dual
union all
select '4,2,Z,A,F'    val from dual
union all
select '1.2,4,E,1,1'  val from dual
union all
select 'F,1,3.4,3,A'  val from dual
)
select val from s where REGEXP_LIKE(val, '(^|,)[[:digit:]](,|$)');

标签: sqloracleregexp-like

解决方案


'^([0-9.]*,)([0-9.]*,)([A-Z]+)'如果您只想返回与模式匹配的行,这个正则表达式应该可以工作number, number, word, anything

select val from s where REGEXP_LIKE(val, '^([0-9.]*,)([0-9.]*,)([A-Z]+)');

推荐阅读