简体   繁体   English

使用MySQL从字符串中删除包含特定字符的字符串部分

[英]Remove part of string including a specific character from a string using MySQL

STACK\HYUUM.ROOOO

I need to remove the characters left to 我需要删除留下的字符

'\'

and the result should be 结果应该是

HYUUM.ROOOO

Please Help 请帮忙

According to the documentation : 根据文件

 SUBSTRING_INDEX(str,delim,count)

Returns the substring from string str before count occurrences of the delimiter delim. 在分隔符delim的计数出现之前,从字符串str返回子字符串。 If count is positive, everything to the left of the final delimiter (counting from the left) is returned. 如果count为正数,则返回最终分隔符左侧的所有内容(从左侧开始计算)。 If count is negative, everything to the right of the final delimiter (counting from the right) is returned. 如果count为负数,则返回最终分隔符右侧的所有内容(从右侧开始计算)。 SUBSTRING_INDEX() performs a case-sensitive match when searching for delim. 搜索delim时,SUBSTRING_INDEX()执行区分大小写的匹配。

In your example, str is 'STACK\\HYUUM.ROOOO'. 在您的示例中,str是'STACK \\ HYUUM.ROOOO'。 Be careful with '\\', it must be escaped because it's a special character. 小心'\\',它必须被转义,因为它是一个特殊字符。 To do that, replace '\\' by '\\\\'. 为此,请将'\\'替换为'\\\\'。 delim is '\\\\' (escaped too) and count is -1 because you want the right part of the delim. delim是'\\\\'(也是转义),count是-1,因为你想要正确的delim部分。

Example : 示例:

mysql> SELECT * FROM foo;
+-------------------+
| name              |
+-------------------+
| STACK\HYUUM.ROOOO |
+-------------------+
1 row in set (0.00 sec)

Then 然后

mysql> SELECT SUBSTRING_INDEX(name, '\\', -1) AS foo FROM foo;
+-------------+
| foo         |
+-------------+
| HYUUM.ROOOO |
+-------------+
1 row in set (0.00 sec)

Or, a simpler example : 或者,一个更简单的例子:

SELECT SUBSTRING_INDEX('STACK\\HYUUM.ROOOO', '\\', -1);

Don't forget to escape the backslash in 'STACK\\HYUUM.ROOOO'. 不要忘记逃避'STACK \\ HYUUM.ROOOO'中的反斜杠。

试试:

 SUBSTRING_INDEX('STACK\HYUUM.ROOOO', '\\', -1)
REPLACE('STACK\HYUUM.ROOOO','\\','');

ups mistake. 错误。 I'll try to find solution. 我会尽力找到解决方案。

I got the it :) 我明白了:)

select employee_id,emp_email,SUBSTRING_INDEX(user_name, '\\', -1) 
from employee_master

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM