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

前端 未结 4 1209
轻奢々
轻奢々 2020-12-21 01:45
STACK\\HYUUM.ROOOO

I need to remove the characters left to

\'\\\'

and the result should be

HYU         


        
相关标签:
4条回答
  • 2020-12-21 02:17
    REPLACE('STACK\HYUUM.ROOOO','\\','');
    

    ups mistake. I'll try to find solution.

    0 讨论(0)
  • 2020-12-21 02:18

    I got the it :)

    select employee_id,emp_email,SUBSTRING_INDEX(user_name, '\\', -1) 
    from employee_master
    
    0 讨论(0)
  • 2020-12-21 02:19

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

    0 讨论(0)
  • 2020-12-21 02:22

    Try with:

     SUBSTRING_INDEX('STACK\HYUUM.ROOOO', '\\', -1)
    
    0 讨论(0)
提交回复
热议问题