Search code examples
sqloracle11gregexp-like

What is the difference between following SQL queries containing REGEXP_LIKE()?


select distinct first_name 
from EMPLOYEES 
where regexp_like(first_name,'^[^AEIOU]*[^aeiou]$');    

select distinct first_name 
from EMPLOYEES 
where regexp_like(first_name,'^[^AEIOU].*[^aeiou]$');  

I am trying to find Employee's first name's that doesn't start with and end with a vowel. I came up with above queries. Now I have two questions:

  1. Do the above statements return valid output (doesn't start and with vowel).

  2. Do the above statements return same result always (I get same result when I tried).

But when I tried the below two queries they gave different outputs with respect to each other

select distinct first_name 
from EMPLOYEES 
where regexp_like(first_name,'^[AEIOU]*[aeiou]$');    

select distinct first_name 
from EMPLOYEES 
where regexp_like(first_name,'^[AEIOU].*[aeiou]$');         

Solution

  • 1) The two first queries don't give you valid ouput. They match names that start with lowercase vowels or end with uppercase vowels. And they don't give always the same result:

    • Marcos is a match for the first and the second
    • MARCOS is a match for the second and not the first
    • allan is a match for both

    2) The second pair presents different output for similar reasons.

    You can try it yourself: Regular expressions 101