Wednesday, January 30, 2008

Oracle Regular Expressions in 10G

I was requested to find all non-numeric values in the column ATTRIBUTE1 of table PA_BUDGET_LINES in 11i. Since the table has more than one million rows, I had to find a good query to do the task.

The new regular expressions helped me to get the job done quickly:

select * from pa_budget_lines
where attribute1 is not null
and (regexp_like(upper(attribute1), '*.[A-Z].*') or regexp_like(attribute1, '[^A-Z,0-9,.,-]'));
-- regexp_like(upper(attribute1), '[^0-9]') does not include the mix.

There are four functions available in both SQL and PL/SQL:

REGEXP_LIKE -- Determine whether pattern matches
REGEXP_SUBSTR -- Determine what string matches the pattern
REGEXP_INSTR -- Determine where the match occurred in the string
REGEXP_REPLACE -- Search and replace a pattern

No comments: