帮助!
我正在尝试将下面的字符串修剪到中间部分"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
所有帮助&非常感谢您的建议!
我会使用漂亮的SUBSTRING_INDEX
函数来做这样的事情。
参考:https://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_substring-索引
例如:
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