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

问题描述 投票:0回答:2

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

 ---------------
|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   |
 ---------
mysql sql database replace trim
2个回答
6
投票

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

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

0
投票

一次修剪多个字符怎么样?

 SELECT colName, TRIM(colName,'North South East West') as MyNew.colName
    FROM DataTable
© www.soinside.com 2019 - 2024. All rights reserved.