开发者

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, '开发者_Python百科('))

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?


Try this :

SELECT SUBSTRING(cityname, CHARINDEX('(',cityname)+1,
           (LEN(cityname) - CHARINDEX('(',cityname)-1)) 
FROM cities WHERE cityname LIKE '(%)%' 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜