4
STACK\HYUUM.ROOOO

I need to remove the characters left to

'\'

and the result should be

HYUUM.ROOOO

Please Help

4 Answers 4

6

According to the documentation :

 SUBSTRING_INDEX(str,delim,count)

Returns the substring from string str before count occurrences of the delimiter delim. If count is positive, everything to the left of the final delimiter (counting from the left) is returned. If count is negative, everything to the right of the final delimiter (counting from the right) is returned. SUBSTRING_INDEX() performs a case-sensitive match when searching for delim.

In your example, str is '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.

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'.

Sign up to request clarification or add additional context in comments.

2 Comments

You are right. But when we give SELECT SUBSTRING_INDEX('STACK\HYUUM.ROOOO', '\\', -1) like this the result is STACKHYUUM.ROOOO
@jennifer When the value exists on the database, the '\' is already escaped. if you do this like your comment, you must escape the '\'. You have to replace SELECT SUBSTRING_INDEX('STACK\HYUUM.ROOOO', '\\', -1) by SELECT SUBSTRING_INDEX('STACK\\HYUUM.ROOOO', '\\', -1) (see the backslash).
3

Try with:

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

2 Comments

when i give select SUBSTRING_INDEX('STACK\HYUUM.ROOOO', '\\', -1) ; i got STACKHYUUM.ROOOO. i need HYUUM.ROOOO
but when i select a column name by giving select SUBSTRING_INDEX(user_name, '\\', -1) from employee_master .. It works
1
REPLACE('STACK\HYUUM.ROOOO','\\','');

ups mistake. I'll try to find solution.

3 Comments

This combined with SUBSTRING_INDEX() to match everything left of \ should work out. EDIT: Come to think of it, it might be the whole solution :-P
i give select REPLACE('STACK\HYUUM.ROOOO','\\',''); the ouput is STACKHYUUM.ROOOO
yes I know I'm trying to find solution. I thought you needed to replace only a backslash =)
1

I got the it :)

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.