Kosh
Kosh

Reputation: 1246

Is it possible to combine LIKE conditions in SQL?

My code:

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%');

Is it possible to combine the LIKE conditions to something less cumbersome ? In IN % does not work therefore I can not use

SELECT DISTINCT city FROM station WHERE city IN ('A%', 'E%', 'I%', 'O%', 'U%')

I know that I can use

SELECT city FROM station WHERE LEFT(city,1) IN ('a', 'e', 'i', 'o', 'u');

but I an just curios about LIKE usage.

P.S. I am being studying SQL without being tied to any specific database management system.

Upvotes: 0

Views: 43

Answers (2)

Rahul Singh
Rahul Singh

Reputation: 710

You can use regex

WHERE city REGEXP '^[aeiou]'

^ Beginning of string

[...] Any character listed between the square brackets

*Tried this on Mysql 5.7.26

Upvotes: 1

Shantanu Kher
Shantanu Kher

Reputation: 1054

you can use REGEXP_LIKE. Sample code -

SELECT DISTINCT city FROM station WHERE REGEXP_LIKE(city,'^a|^e|^i|^o|^u','i');

Upvotes: 1

Related Questions