Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable escape characters in a MySQL query

Tags:

mysql

Is there a way to disable escape characters in a MySQL query? For example, for the following table:

mysql> select * from test1;
+------------------------+-------+
| name                   | value |
+------------------------+-------+
| C:\\media\data\temp\   |     1 |
| C:\\media\data\temp    |     2 |
| /unix/media/data/temp  |     3 |
| /unix/media/data/temp/ |     4 |
+------------------------+-------+

I want the following to be a valid query:

mysql> select * from test1 where name='C:\\media\data\temp\';

I know that I can instead use

mysql> select * from test1 where name='C:\\\\media\\data\\temp\\';

But I am building this query using my_snprintf(), so there instead I have to use

C:\\\\\\\\media\\\\data\\\\temp\\\\

...and so on! Is there a way to disable escape characters for a single MySQL query ?

like image 916
Sagar Jauhari Avatar asked Feb 24 '12 09:02

Sagar Jauhari


People also ask

How do I remove special characters from a MySQL query?

You can remove special characters from a database field using REPLACE() function. The special characters are double quotes (“ “), Number sign (#), dollar sign($), percent (%) etc.

How do I ignore an escape character in a string?

An escape sequence is a set of characters used in string literals that have a special meaning, such as a new line, a new page, or a tab. For example, the escape sequence \n represents a new line character. To ignore an escape sequence in your search, prepend a backslash character to the escape sequence.

How do I remove a specific character from a string in MySQL?

Remove characters from string using TRIM() TRIM() function is used to remove any character/ whitespace from the start/ end or both from a string.

How do I change a special character with a space in MySQL?

SELECT REGEXP_REPLACE(columnName, '[^\\x20-\\x7E]', '') from tableName; Count to do a safety check ... Then update This update is a catch all after the mapping update. Change the limit number to the count value above ...


1 Answers

You can disable backslash escapes by setting NO_BACKSLASH_ESCAPES in the SQL mode:

-- save mode & disable backslashes
SET @old_sql_mode=@@sql_mode;
SET @@sql_mode=CONCAT_WS(',', @@sql_mode, 'NO_BACKSLASH_ESCAPES');

-- run the query
SELECT 'C:\\media\data\temp\';

-- enable backslashes
SET @@sql_mode=@old_sql_mode;
like image 181
outis Avatar answered Sep 25 '22 23:09

outis