修剪字符串MYSQL的多个尾随和前导关键字

时间:2016-12-07 12:18:52

标签: mysql sql database replace trim

我有一个包含类似数据的表

 ---------------
|town           |
 ---------------
|Longton North  |
|Longton South  |
|Longton North  |
|Longton East   |
|Longton West   |
|East Valley    |
|West Valley    |
 ---------------

我知道如何使用

修剪字符串中的尾部和前导字符
TRIM(BOTH 'North' FROM town)

但是我想从结果中削减北,南,西,东。所以输出应该像

 ---------
|town     |
 ---------
|Longton  |
|Longton  |
|Longton  |
|Longton  |
|Longton  |
|Valley   |
|Valley   |
 ---------

1 个答案:

答案 0 :(得分:2)

试试这个...这对你有用..

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