Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sql

select

mysql

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?

like image 788
Zeus Avatar asked Apr 14 '16 15:04

Zeus


People also ask

How do you select a city that starts a vowel in SQL?

select city from station where REGEXP_LIKE(city,'[^aeiou]+');

How do you use begin AND end in SQL?

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.

How do you print a vowel string in SQL?

Try using and name LIKE '%a%' for your vowels, this will search the entire string and not just the last letter.


2 Answers

You could use a regular expression:

SELECT DISTINCT city FROM   station WHERE  city RLIKE '^[aeiouAEIOU].*[aeiouAEIOU]$' 
like image 70
Mureinik Avatar answered Sep 22 '22 15:09

Mureinik


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'); 
like image 34
Banketeshvar Narayan Avatar answered Sep 23 '22 15:09

Banketeshvar Narayan