Oracle – How to Match Regular Expressions

oracle

I'm using the following query:

select * from table
WHERE POSTAL_CODE LIKE '[a-Z][0-9][a-Z] [0-9][a-Z][0-9]';

However, this is not capturing anything. For example, a typical POSTAL_CODE looks like 'B3M 3K5'. Why would this not match?

Best Answer

You'd need to use regexp_like not like if you want to use regular expressions. It sounds like you want

with x as (
    select 'A1B C2D' postal_code from dual union all
    select 'XXX 777' from dual union all
    select 'a1B 2C3' from dual
 )
 select *
   from x
  where regexp_like( postal_code, '^[A-Z][0-9][A-Z] [0-9][A-Z][0-9]$', 'i' );