对不同字符长度varchar的TRIM查询

时间:2015-06-03 19:27:47

标签: mysql sql varchar

帮助!

我正在尝试将下面的字符串修剪为中间部分“MS Lync”

“系统和应用 - > MS Lync - >请求更改”

我尝试过以下查询,但仍然会获得特殊字符。字符串长度会改变,但特殊字符不会改变:

LEFT(probcodedesc, CHARINDEX('-', probcodedesc)) AS Area,
substring(probcodedesc, charindex('> ',probcodedesc),charindex('> ', probcodedesc, charindex('>', probcodedesc) - charindex(' ->', probcodedesc))) as Category,
RIGHT(probcodedesc, CHARINDEX('>', REVERSE(probcodedesc))) AS Event

所有帮助&建议赞赏!

1 个答案:

答案 0 :(得分:1)

我使用漂亮的SUBSTRING_INDEX函数来做这样的事情。

参考:https://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_substring-index

例如:

SET @s = 'Systems & Apps -> MS Lync -> Request for Change';

SELECT TRIM(SUBSTRING_INDEX(@s,'->',1)) AS `Area`

Area
----------------
Systems & Apps  


SELECT TRIM(SUBSTRING_INDEX(SUBSTRING_INDEX(@s,'->',-2),'->',1)) AS `Category`

Category       
---------
MS Lync  


SELECT TRIM(SUBSTRING_INDEX(@s,'->',-1)) AS `Event`

Event
------------------
Request for Change