Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Group By a substring in a field

I have a cities table in which I have citynames like "Los Angeles (California)".

I tried to extract all the values contained between parantheses with a request, but I couldn't manage to do it well...

The request I tried looked like this :

SELECT cityname FROM cities WHERE cityname LIKE "%(%)%" 
GROUP BY SUBSTR(cityname, FIND_IN_SET(cityname, '('))

Or something like (without group by):

SELECT SUBSTR(cityname, FIND_IN_SET(cityname, '(')) FROM cities 
WHERE cityname LIKE "%(%)%"

What seems to be wrong with these requests?

like image 500
B F Avatar asked Jan 26 '26 18:01

B F


1 Answers

Try this :

SELECT SUBSTRING(cityname, CHARINDEX('(',cityname)+1,
           (LEN(cityname) - CHARINDEX('(',cityname)-1)) 
FROM cities WHERE cityname LIKE '(%)%' 
like image 198
Upendra Chaudhari Avatar answered Jan 29 '26 08:01

Upendra Chaudhari