I want to match the string having escape characters with particular column in a table.
SELECT * FROM table WHERE col LIKE 'MESSRESTAURANGER AB\\MESSVEGEN 1\\STOCKH';
Though there is matching data in table, query result is empty set. But the same query works fine in oracle. What is the issue with mysql?
You miss %:
SELECT * FROM table WHERE col LIKE '%MESSRESTAURANGER AB\\MESSVEGEN 1\\STOCKH%';
But it should work without escaping:
SELECT * FROM table WHERE col LIKE '%MESSRESTAURANGER AB\MESSVEGEN 1\STOCKH%';
Fiddle http://sqlfiddle.com/#!9/a7ba59/2
EDIT:
SELECT * FROM t WHERE n LIKE '%MESSRESTAURANGER AB\\\\\\\\MESSVEGEN 1\\\\\\\\STOCKH%'
Because MySQL uses C escape syntax in strings (for example, “\n” to
represent a newline character), you must double any “\” that you use
in LIKE strings. For example, to search for “\n”, specify it as “\n”.
To search for “\”, specify it as \\\\; this is because the
backslashes are stripped once by the parser and again when the pattern
match is made, leaving a single backslash to be matched against.
Fiddle http://sqlfiddle.com/#!9/ac46b/9
Related
I have to find all the values of a specific column in a table where the column values match like [name]
I should not get the values that are like [a]+[b] or [a]>[b] or [a]%[b]=[c]
So I basically do not need column values that have special characters in them except the square brackets and under score
example: [test_123] should be returned.
I tried
select * from table_name where column_name REGEXP '^[[][^+-></%]';
This is just trying to see if there is any special character immediately after [ but how to see if there is any special character in the whole column value and should we give backslashes to define special characters in MySQL?
I tried in https://regexr.com/ and I have got my required Regex to be
(\[)\w+[^\+\=\/\*\%\^\!](\])
but I could not do the same in MySQL
*** UPDATED **
(\[[^\_]+])+
That seems to work for what you're looking for.
Also your query is wrong, I believe it is supposed to look like:
SELECT * FROM mytable WHERE REGEXP_LIKE(mycolumn, 'regexp', 'i');
I have 3 strings entries in my table sample and column test
1.abc
2.abc%d
3.abc%E
Now I want to write a query to fetch all the records in column test that contains abc% using like condition. The output should be abc%d and abc%E.
Have you used the "_" underscore character in SQL query?
This may help to resolve your issue.
select * from myTableName where details like 'abc%_%'
or
select * from myTableName where details like 'abc%_'
or
select * from myTableName where details LIKE '%abc\%%' ESCAPE '\'
or
select * from myTableName where details LIKE 'abc\%%' ESCAPE '\'
All the above queries will solve your issue, use the appropriate query based on your application need and requirement.
Reference: Use Underscore character in wild card charecter of Like query gives me all table result
As stated in the documentation, you must escape instances of wildcard characters if you do not want them to behave as such.
To test for literal instances of a wildcard character, precede it by
the escape character. If you do not specify the ESCAPE character, \ is
assumed.
\% matches one % character.
\_ matches one _ character.
For example, Search is a commonly used functions.
I will generate a query string like "SELECT * FROM table WHERE col LIKE '%{keyword}%'"
but the keyword's value is '%', it does not work, how to fix it.
In addition, what other characters need to be aware of?
Just use a backslash to escape the percent. For example, if your keyword were 5% discount, then you would use:
SELECT * FROM table WHERE col LIKE '%5\% discount%'
The documentation lists only two characters which have a special meaning in LIKE:
% - matches any number of characters
_ - matches any single character
If you want to use either of these characters literally in a LIKE expression, then you will need to escape them.
I have one table emp in MySQL database having column as name. In that name column, the value is 'abc\xyz'. I want to search this value. I have tried using following query:
select * from emp where name like 'abc\xyz';
Also i have tried
select * from emp where name like 'abc\xyz' escape '\\';
But i did not found any output. Could you please help me in finding such strings? Such strings can have special character at any location.
Thanks in advance.
You may try like this:
select * from emp
where empname like '%abc\\\\xyz%'
SQL Fiddle Demo
From the docs:
Because MySQL uses C escape syntax in strings (for example, “\n” to represent a newline character), you must double any “\” that you use in LIKE strings. For example, to search for “\n”, specify it as “\\n”. To search for “\”, specify it as “\\\\”; this is because the backslashes are stripped once by the parser and again when the pattern match is made, leaving a single backslash to be matched against.
SELECT REPLACE(text,'\\','') FROM tbl
You can use REPLACE to remove some special chars :)
SELECT *
FROM list
WHERE list_item LIKE "%_1375_%"
returns results it should not. For example a result with 13753 in the list_item instead of 1375, even though the 3 comes before the underscore.
_13753_ and _1375_ are written above and not suppose to be italic.
Is there anyway to fix that?
From MySQL reference manual here
SQL pattern matching enables you to use “_” to match any single
character and “%” to match an arbitrary number of characters
(including zero characters).
So, you'll have to escape them to match a literal underscore character. Like so:
SELECT * FROM list
WHERE list_item LIKE "%\_1375\_%"
Underscore means any character. Are you looking for values with an underscore? In that case you need to escape it
SELECT *
FROM list
WHERE list_item LIKE "%\_1375\_%"
Try this one
LIKE '%[_]1375[_]%'