5

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';
Dharman
  • 30,962
  • 25
  • 85
  • 135

17 Answers17

5

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')
fhossfel
  • 2,041
  • 16
  • 24
2

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
Tzu7
  • 73
  • 6
1

This will be answerable in SQL Server in 3 lines with an AND clause-

SELECT DISTINCT CITY FROM *TableName* WHERE
(city like 'a%' or city like 'e%' or city like 'i%' or city like 'o%' or city like 'u%') 
AND 
(city like '%a' or city like '%e' or city like '%i' or city like '%o' or city like '%u');

...

Also, you can use Regex in case of MySQL as-

SELECT DISTINCT city
FROM   station
WHERE  city RLIKE '^[aeiouAEIOU].*[aeiouAEIOU]$'
David Buck
  • 3,752
  • 35
  • 31
  • 35
0

I'm not an expert in DB2, but I think you can use a regular expression in your WHERE LIKE.

Check the documentation: https://www.ibm.com/support/knowledgecenter/en/SSEPGG_11.1.0/com.ibm.db2.luw.sql.ref.doc/doc/r0061494.html

Example:
Select Distinct city
From   station
Where  REGEXP_LIKE(city,'[aeiou].*[aeiou]')
Max Hanglin
  • 186
  • 1
  • 5
0

Use regular expressions in your LIKE clause. Look at the example here Using RegEx in SQL Server

In Sql server your LIKE clause might look like this.

LIKE '[AEIOU]%[aeiou]'

In DB2 the syntax is slightly different. Look few examples down on this page.

https://www.ibm.com/developerworks/data/library/techarticle/0301stolze/0301stolze.html

Dharman
  • 30,962
  • 25
  • 85
  • 135
Aamir Mulla
  • 76
  • 1
  • 6
0

You could use substr like this:

SELECT DISTINCT CITY 
FROM STATION WHERE SUBSTR(CITY,1,1) IN('A','E','I','O','U','a','e','i','o','u') 
and SUBSTR(CITY,-1,1) IN('A','E','I','O','U','a','e','i','o','u');
trincot
  • 317,000
  • 35
  • 244
  • 286
0
SELECT DISTINCT CITY FROM STATION WHERE REGEXP_LIKE(CITY,'^[^aeiouAEIOU].*');
Dharman
  • 30,962
  • 25
  • 85
  • 135
0

Try the following in Oracle:

SELECT DISTINCT CITY FROM STATION WHERE REGEXP_LIKE(CITY,'^[^aeiouAEIOU].*');
Dharman
  • 30,962
  • 25
  • 85
  • 135
0
select distinct city from station where REGEXP_LIKE(city,'[aeiou]$');
Syscall
  • 19,327
  • 10
  • 37
  • 52
0

For Oracle, you can write this-

SELECT * FROM(
    SELECT UNIQUE(city) FROM station WHERE
    LOWER(SUBSTR(city,1,1)) IN ('a','e','i','o','u')
    INTERSECT  
    SELECT UNIQUE(city) FROM station WHERE
    LOWER(SUBSTR(city,LENGTH(city),1)) IN ('a','e','i','o','u')
);
Mahedi Kamal
  • 179
  • 1
  • 9
0
SELECT DISTINCT CITY FROM STATION WHERE LOWER(LEFT(CITY,1)) IN ('a', 'e', 'i', 'o', 'u') AND LOWER(RIGHT(CITY,1)) IN ('a', 'e', 'i', 'o', 'u');
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
0

Maybe this code will work.

Select Distinct City from station
Where city Like '[aeiou]%[aeiou]'

This worked for me.

cigien
  • 57,834
  • 11
  • 73
  • 112
0

In MSSQL server code working fine

select distinct city from station where city like '[aeiouAEIOU]%[aeiouAEIOU]';
bunyaminkirmizi
  • 540
  • 2
  • 6
  • 22
0
select distinct CITY from STATION where substr(CITY,1,1)  in ('a','e','i','o','u') and substr(city,-1,1) in ('a','e','i','o','u');
Dharman
  • 30,962
  • 25
  • 85
  • 135
0

This is working in my case

select distinct(city) from station where city LIKE '%e' or city LIKE '%a' or city LIKE '%i' or city LIKE '%o' or city LIKE '%u';
0

I solved this with the query,

SELECT DISTINCT CITY FROM STATION WHERE REGEXP_LIKE(CITY,'^[aeiouAEIOU]');

-1

As a beginner, this is how I solved it:

SELECT DISTINCT city 
FROM station
WHERE
city LIKE 'a%' OR
city LIKE 'e%' OR
city LIKE 'i%' OR
city LIKE 'o%' OR
city LIKE 'u%';