Skip to Main Content

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question.

Asked: December 02, 2016 - 6:21 pm UTC

Last updated: December 04, 2016 - 1:25 pm UTC

Version: Oracle 11g

Viewed 1000+ times

You Asked

I need to write a query which will look for the data in a particular column for the strings of patterns nnnnnn or Annnnn where n stands for number and A stands for capital letter. Any strings which are in a different pattern other than these two should be filtered or displayed.

Thanks

and Connor said...

Something like this perhaps ?

SQL> with t as
  2  (
  3  select 'a123' x from dual union all
  4  select 'A123' x from dual union all
  5  select 'F123' x from dual union all
  6  select 'A12d' x from dual )
  7  select *
  8  from t
  9  where regexp_like(x,'[A-Z][[:digit:]]+$');

X
----
A123
F123


You can adjust the regexp to tighten up or loosen the rules to suit your needs.

Rating

  (3 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

A reader, December 04, 2016 - 3:42 pm UTC

Can we write something using like operator instead of regex function as they use very high cpu.

without Regexp

Rajeshwaran, Jeyabal, December 05, 2016 - 10:24 am UTC

may be something like this ?

demo@ORA12C> with t as
  2    (
  3    select 'a123' x from dual union all
  4    select 'A123' x from dual union all
  5    select 'F123' x from dual union all
  6    select 'A12d' x from dual )
  7    select *
  8    from t
  9    where replace( translate(x,'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ','$') ,'$') is null
 10  /

X
----
A123
F123

2 rows selected.

demo@ORA12C>

do not meet the need : one char followed by number

gh, December 05, 2016 - 10:55 am UTC

with t as
(
select 'a123' x from dual union all
select 'A123' x from dual union all
select 'FF123' x from dual union all
select 'F123' x from dual union all
select 'A12d' x from dual )
select *
from t
where coalesce ( trim(translate(substr(x,1,1),'ABCDEFGHIJKLMNOPQRSTUVWXYZ',' ')), trim( translate(substr(x,2),'0123456789',' ')) )is null