I wrote this code (db2) and it works just fine, but I'm wondering, is there a shorter way to write this?
Select Distinct city
From station
Where city Like 'A%a'
Or city Like 'A%e'
Or city Like 'A%i'
Or city Like 'A%o'
Or city Like 'A%u'
Or city Like 'E%a'
Or city Like 'E%e'
Or city Like 'E%i'
Or city Like 'E%o'
Or city Like 'E%u'
Or city Like 'I%a'
Or city Like 'I%e'
Or city Like 'I%i'
Or city Like 'I%o'
Or city Like 'I%u'
Or city Like 'O%a'
Or city Like 'O%e'
Or city Like 'O%i'
Or city Like 'O%o'
Or city Like 'O%u'
Or city Like 'U%a'
Or city Like 'U%e'
Or city Like 'U%i'
Or city Like 'U%o'
Or city Like 'U%u';
I am not a DB2 expert but this should be fairly portable:
WHERE LEFT(city,1) IN ('A', 'E', 'I', 'O', 'U')
AND RIGHT(city,1) IN ('a', 'e', 'i', 'o', 'u')
You may want to normalize it all to upper case to avoid problems with cities that for some reason start with a lower case letter or end with an upper case letter.
WHERE UPPER(LEFT(city,1)) IN ('A', 'E', 'I', 'O', 'U')
AND LOWER(RIGHT(city,1)) IN ('a', 'e', 'i', 'o', 'u')
You can use REGEXP
in MySQL to operate Regular Expression
SELECT DISTINCT city
FROM station
WHERE city REGEXP '^[aeiou].*[aeiou]$';
For people who don't familiar with Regular Expression
^[aeiou] // Start with a vowel
.* // Any characters at any times
[aeiou]$ // End with a vowel
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