Remove part of a string, including a specific character from a string, using MySQL

STACK\HYUUM.ROOOO 

I need to delete characters left before

 '\' 

and the result should be

 HYUUM.ROOOO 

Please, help

+4
source share
4 answers

I got it:)

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

According to the documentation :

  SUBSTRING_INDEX(str,delim,count) 

Returns the substring from str before counting delim delimiter attachments. If the counter is positive, everything is returned to the left of the trailing separator (counting from the left). If the counter is negative, everything that is to the right of the final separator (counting to the right) is returned. SUBSTRING_INDEX () is case-sensitive when looking for a delimiter.

In your example str is "STACK \ HYUUM.ROOOO". Be careful with '\', this must be escaped because it is a special character. To do this, replace '\' with '\\'. delim is '\\' (shielded too), and count is -1 because you want the right side of 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); 

Remember to avoid the backslash in 'STACK \ HYUUM.ROOOO'.

+6
source

Try:

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

error of errors. I will try to find a solution.

+1
source

Source: https://habr.com/ru/post/1346724/


All Articles