如何获取列数据以特定字符结尾并后跟数字的数据

时间:2017-06-16 11:56:28

标签: sql oracle oracle11g

我的表REMARKS中有一个名为TBL_DATA的列,我的数据如表所示。

REMARKS
--------------
Ramesh has VR2    
Ravi is VR1 member    
Rajesh had VR2    
Rakesh is VR10 employee

我的要求是从remarks结尾的表中获取数据,其中VR后跟VR2, VR3,VR10,VR33之类的数字。

从上面的数据我想得到第一和第三行数据。

P.S:我不应该使用类似的运算符(LIKE'%VR%')

3 个答案:

答案 0 :(得分:1)

您可能需要以下内容:

with tbl_data(remarks) as (
    select 'Ramesh has VR2' from dual union all
    select 'Ravi is VR1 member' from dual union all
    select 'Rajesh had VR2' from dual union all
    select 'Rakesh is VR10 employee' from dual
)
select *
from tbl_data
where regexp_like(remarks, 'VR[0-9]+$') 

给出:

REMARKS
-----------------------
Ramesh has VR2
Rajesh had VR2

'VR[0-9]+$'如何运作:

  • VR:不言自明
  • [0-9]+:一个或多个数字
  • $:字符串的结尾

答案 1 :(得分:0)

只需使用正则表达式:

select t.*
from t
where regexp_like(t.remarks, 'VR[0-9]+$');

答案 2 :(得分:0)

另一个正则表达式:

with tbl_data(remarks) as ( select 'Ramesh has VR2' from dual union all select 'Ravi is VR1 member' from dual union all select 'Rajesh had VR2' from dual union all select 'Rakesh is VR10 employee' from dual ) select * from tbl_data where regexp_like(remarks, 'VR\d+$') ;