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 characters. The result cannot contain duplicates.
For this is I wrote a query like WHERE NAME LIKE 'a%'
that had 25 conditions, each vowel for every other vowel, which is quite unwieldy. Is there a better way to do it?
select city from station where REGEXP_LIKE(city,'[^aeiou]+');
BEGIN... END blocks can be nested. Although all Transact-SQL statements are valid within a BEGIN... END block, certain Transact-SQL statements should not be grouped together within the same batch, or statement block.
Try using and name LIKE '%a%' for your vowels, this will search the entire string and not just the last letter.
You could use a regular expression:
SELECT DISTINCT city FROM station WHERE city RLIKE '^[aeiouAEIOU].*[aeiouAEIOU]$'
in Microsoft SQL server you can achieve this from below query:
SELECT distinct City FROM STATION WHERE City LIKE '[AEIOU]%[AEIOU]'
Or
SELECT distinct City FROM STATION WHERE City LIKE '[A,E,I,O,U]%[A,E,I,O,U]'
Update --Added Oracle Query
--Way 1 --It should work in all Oracle versions
SELECT DISTINCT CITY FROM STATION WHERE REGEXP_LIKE(LOWER(CITY), '^[aeiou]') and REGEXP_LIKE(LOWER(CITY), '[aeiou]$');
--Way 2 --it may fail in some versions of Oracle
SELECT DISTINCT CITY FROM STATION WHERE REGEXP_LIKE(LOWER(CITY), '^[aeiou].*[aeiou]');
--Way 3 --it may fail in some versions of Oracle
SELECT DISTINCT CITY FROM STATION WHERE REGEXP_LIKE(CITY, '^[aeiou].*[aeiou]', 'i');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With