Search of String value having special character in MySQL - mysql

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 :)

Related

SQL - match last two characters in a string

I have a small mysql database with a column which has format of a field as following:
x_1_1,
x_1_2,
x_1_2,
x_2_1,
x_2_12,
x_3_1,
x_3_2,
x_3_11,
I want to extra the data where it matches last '_1'. So if I run a query on above sample dataset, it would return
x_1_1,
x_2_1,
x_3_1,
This should not return x_2_12 or x_3_11.
I tried like '%_1' but it returns x_2_12 and x_3_11 as well.
Thank you!
A simple method is the right() function:
select t.*
from t
where right(field, 2) = '_1';
You can use like but you need to escape the _:
where field like '%$_1' escape '$'
Or use regular expressions:
where field regexp '_1$'
The underscore character has special significance in a LIKE clause. It acts as a wildcard and represent one single character. So you would have to escape it with a backslash:
LIKE '%\_1'
RIGHT does the job too, but it requires that you provide the proper length for the string being sought and is thus less flexible.
Duh, I found the answer.
Use RIGHT (col_name, 2) = '_1'
Thank you!

mysql search where column like ''

Quick questions, I bet the answer is so simple and I am just being blind.
I want to select from the database all the names that only start with "test_1_1_".
I would guess that I do this with;
SELECT * FROM my_table WHERE names LIKE "test_1_1_%";
This doesn't seem to work.
The results keep showing up as
test_1_1_1
test_1_1_2
test_1_11_1
test_1_11_2
test_1_12_1
test_1_12_2
How can I select with MySQL only the results that start with "test_1_1_"?
Thank you in advance.
Wesley
Underscore has a special meaning in LIKE and needs to be escaped:
SELECT * FROM my_table WHERE names LIKE 'test\_1\_1\_%';
You could also use REGEXP here, and avoid the escaping problem:
SELECT * FROM my_table WHERE names REGEXP '^test_1_1_';
When you use _ inside LIKE, it will mean replace that space with any character.
For example, searching for something like field LIKE 'a_' will result in any field with 2 characters starting with "a".
If you really want to search for the underscore characters, you need to escape the value with \ and your query will look like this: LIKE 'test\_1\_1\_%';
The underscore is a single character wildcard when use with LIKE. So to specifically locate an underscore it needs to be "escaped" as follows:
select
*
from mytable
where names like 'test\_1\_1\_%' escape '\'
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.
https://dev.mysql.com/doc/refman/8.0/en/string-comparison-functions.html
Can do this:
SELECT * FROM my_table WHERE names LIKE "test\_1\_\1\_%";

How to use like condition in query to fetch strings that contains % symbol

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.

matching escape charactres using like operator in mysql

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

How do apply SQL like on "\detail1\detail2\" (Escaping '\')?

I have a table T1 with a column details, data column has following sample data :
select details from T1;
\this is detail 1\this is detail2\
\this is detail 1\this is detail2\
:this is detail 1:this is detail2:
:this is detail 1:this is detail2:
\this is detail 1\this is detail2\
I want to fetch only those details which are separated by \.
For that I wrote the following query :
select details from T1 where details like '\%\%\';
But its not responding as expected, since its taking \ as escape corrector.
So, its showing error of incomplete single inverted(') !
So, how to do this ?
Try this, you need to escape backslashes twice in LIKE statement.
select details from T1 where details like '\\\\%\\\\%\\\\'
http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html
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.
MySql has escape character \\ for backslash (same as in C-like languages):
http://dev.mysql.com/doc/refman/5.0/en/string-syntax.html
Try using '\\%\\%\\' - using a backslash to escape the backslash.