SQL query to check if a name begins and ends with a vowel

后端 未结 25 942
臣服心动
臣服心动 2020-11-30 23:16

I want to query the list of CITY names from the table STATION(id, city, longitude, latitude) which have vowels as both their first and last charact

相关标签:
25条回答
  • 2020-11-30 23:56

    You can use the following regular expression and invert the result:

    ^[^aeiou]|[^aeiou]$
    

    This works even if the input consists of a single character. It should work across different regex engines.

    MySQL

    SELECT city
    FROM (
        SELECT 'xx' AS city UNION
        SELECT 'ax'         UNION
        SELECT 'xa'         UNION
        SELECT 'aa'         UNION
        SELECT 'x'          UNION
        SELECT 'a'
    ) AS station
    WHERE NOT city REGEXP '^[^aeiou]|[^aeiou]$'
    

    PostgreSQL

    WHERE NOT city ~ '^[^aeiou]|[^aeiou]$'
    

    Oracle

    WHERE NOT REGEXP_LIKE(city, '^[^aeiou]|[^aeiou]$')`
    

    SQL Server

    No regular expression support. Use LIKE clause with square brackets:

    WHERE city LIKE '[aeiou]%' AND city LIKE '%[aeiou]'
    
    0 讨论(0)
  • In Oracle:

    SELECT DISTINCT city 
    FROM station 
    WHERE SUBSTR(lower(CITY),1,1) IN ('a','e','i','o','u') AND SUBSTR(lower(CITY),-1) IN ('a','e','i','o','u');
    
    0 讨论(0)
  • 2020-11-30 23:56

    Try the following:

    select distinct city from station where city REGEXP '^[aeiou]' and city REGEXP '[aeiou]$';
    
    0 讨论(0)
  • 2020-11-30 23:57
    select distinct(city) from STATION 
    where lower(substr(city, -1)) in ('a','e','i','o','u') 
          and lower(substr(city, 1,1)) in ('a','e','i','o','u');
    
    0 讨论(0)
  • 2020-11-30 23:58

    Try the following:

    select distinct city 
    from station 
    where city like '%[aeuio]'and city like '[aeuio]%' Order by City;
    
    0 讨论(0)
  • 2020-11-30 23:59

    The below query will do for Orale DB:

    select distinct(city) from station where upper(substr(city, 1,1)) in ('A','E','I','O','U') and upper(substr(city, length(city),1)) in ('A','E','I','O','U');
    
    0 讨论(0)
提交回复
热议问题