Trim 多个尾随和前导关键字关闭字符串 MYSQL
Trim multiple trailing and leading keyword off string MYSQL
我有一个 table 数据是这样的
---------------
|town |
---------------
|Longton North |
|Longton South |
|Longton North |
|Longton East |
|Longton West |
|East Valley |
|West Valley |
---------------
我知道如何使用
trim 字符串中的尾随和前导字符
TRIM(BOTH 'North' FROM town)
但我想 trim 从我的结果中找出北、南、西、东。所以输出应该像
---------
|town |
---------
|Longton |
|Longton |
|Longton |
|Longton |
|Longton |
|Valley |
|Valley |
---------
试试这个..这对你有用..
select
trim(TRIM(BOTH 'South' FROM TRIM(BOTH 'North' FROM TRIM(BOTH 'East' FROM TRIM(BOTH 'West' FROM town))))) from tbl
或更多指定
select trim(case
when position('North' in town) > 0 then TRIM(BOTH 'North' FROM town)
when position('South' in town) > 0 then TRIM(BOTH 'South' FROM town)
when position('East' in town) > 0 then TRIM(BOTH 'East' FROM town)
when position('West' in town) > 0 then TRIM(BOTH 'West' FROM town) end)
from tbl
我有一个 table 数据是这样的
---------------
|town |
---------------
|Longton North |
|Longton South |
|Longton North |
|Longton East |
|Longton West |
|East Valley |
|West Valley |
---------------
我知道如何使用
trim 字符串中的尾随和前导字符TRIM(BOTH 'North' FROM town)
但我想 trim 从我的结果中找出北、南、西、东。所以输出应该像
---------
|town |
---------
|Longton |
|Longton |
|Longton |
|Longton |
|Longton |
|Valley |
|Valley |
---------
试试这个..这对你有用..
select
trim(TRIM(BOTH 'South' FROM TRIM(BOTH 'North' FROM TRIM(BOTH 'East' FROM TRIM(BOTH 'West' FROM town))))) from tbl
或更多指定
select trim(case
when position('North' in town) > 0 then TRIM(BOTH 'North' FROM town)
when position('South' in town) > 0 then TRIM(BOTH 'South' FROM town)
when position('East' in town) > 0 then TRIM(BOTH 'East' FROM town)
when position('West' in town) > 0 then TRIM(BOTH 'West' FROM town) end)
from tbl