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
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]'
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');
Try the following:
select distinct city from station where city REGEXP '^[aeiou]' and city REGEXP '[aeiou]$';
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');
Try the following:
select distinct city
from station
where city like '%[aeuio]'and city like '[aeuio]%' Order by City;
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');