mysql: replace \ (backslash) in strings

前端 未结 6 801
执念已碎
执念已碎 2021-01-04 11:37

I am having the following problem:

I have a table T which has a column Name with names. The names have the following structure:

A\\\\B\\C

You can cre

相关标签:
6条回答
  • 2021-01-04 11:57

    You could use LIKE:

    SELECT NAME FROM T WHERE NAME LIKE '%\\\\%';
    
    0 讨论(0)
  • 2021-01-04 12:00

    You're confusing what's IN the database with how you represent that data in SQL statements. When a string in the database contains a special character like \, you have to type \\ to represent that character, because \ is a special character in SQL syntax. You have to do this in INSERT statements, but you also have to do it in the parameters to the REPLACE function. There are never actually any double slashes in the data, they're just part of the UI.

    Why do you think you need to double the slashes in the SQL expression? If you're typing queries, you should just double the slashes in your command line. If you're generating the query in a programming language, the best solution is to use prepared statements; the API will take care of proper encoding (prepared statements usually use a binary interface, which deals with the raw data). If, for some reason, you need to perform queries by constructing strings, the language should hopefully provide a function to escape the string. For instance, in PHP you would use mysqli_real_escape_string.

    But you can't do it by SQL itself -- if you try to feed the non-escaped string to SQL, data is lost and it can't reconstruct it.

    0 讨论(0)
  • 2021-01-04 12:01

    The literal A\\B\C must be coded as A\\\\A\\C, and the parameters of replace() need escaping too:

    select 'A\\\\B\\C', replace('A\\\\B\\C', '\\', '\\\\');
    

    output (see this running on SQLFiddle):

    A\\B\C         A\\\\B\\C
    


    So there is little point in using replace. These two statements are equivalent:

    select Name from T where Name = replace('A\\\\B\\C', '\\', '\\\\');
    select Name from T where Name =         'A\\\\B\\C';
    
    0 讨论(0)
  • 2021-01-04 12:11

    Usage of regular expression will solve your problem.

    This below query will solve the given example.

      1) S\\D\B
    
    select * from T where Name REGEXP '[A-Z]\\\\\\\\[A-Z]\\\\[A-Z]$';
    

    if incase the given example might have more then one char

    2) D\\B\ACCC
    
    select * from T where Name REGEXP '[A-Z]{1,5}\\\\\\\\[A-Z]{1,5}\\\\[A-Z]{1,5}$';
    

    note: i have used 5 as the max occurrence of char considering the field size is 10 as its mentioned in the create table query.

    We can still generalize it.If this still has not met your expectation feel free to ask for my help.

    0 讨论(0)
  • 2021-01-04 12:18

    You have to use "verbatim string".After using that string your Replace function will look like this

    Replace(@"\", @"\\")
    

    I hope it will help for you.

    0 讨论(0)
  • 2021-01-04 12:19

    Not exactly sure by what you mean but, this should work.

    select replace('A\\B\C', '\', '\\');
    

    It's basically going to replace \ whereever encountered with \\ :)

    Is this what you wanted?

    0 讨论(0)
提交回复
热议问题