Search '_' in SQL shows all records - mysql

I have a query to search the name with '_', but it shows all records of the table. Would someone tell me what's wrong with my script. Thanks.
There is my SQL script:
declare #Name varchar(50)='_'
select * FROM products WHERE Name like #Name +'%'

_ is a wildcard that matches any character. You can choose another character to escape it. Say:
WHERE Name like replace(#Name, '_', '$_') + '%' ESCAPE '$'
That can get cumbersome, so you can just use different logic:
WHERE LEFT(Name, LEN(#Name)) = #Name
Unfortunately, this formulation prevents the use of an index.

To test for literal instances of a wildcard character, precede it by the escape character. For example.
\_ matches one _ character

Related

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.

Search of String value having special character in 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 :)

underscore '_' character MYSQL

I got a value in database which contains a slash character . /
Eg.
column_name
"ABCDEFGH XYZ IJ/KL"
SQL1
SELECT * FROM `table` WHERE `column_name` LIKE '%XYZ IJ/KL'
SQL2
SELECT * FROM `table` WHERE `column_name` LIKE '%XYZ IJ_KL'
Using SQL 1 , it failed to get the row but SQL2.
Does anyone know why the SQL 2 succeeded?
Thank you.
An underscore, _, in a LIKE expression is a wildcard as deceze mentioned in the comments. It means any single character.
A backslash, \, in a LIKE expression is an escape character.
If you need to match an underscore specifically use \_ and if you need to match a backslash use \\\\.
You do appear to have used a forward slash, /, in your question which does not require escaping but will not match a backslash.. if your DB value contains a forward slash; query SQL1 should return the row.
LIKE comparison docs

Why does this query return too many results?

I have a mysql query
Select * from tbl_schoolphotos where Filename like
'glasshouse_1_%' order by DateTaken
I would like to match
glasshouse_1_20122204123455
glasshouse_1_20122206102415
but not
glasshouse_18_20122206102415
but its matching all three. Anyone know why? Does _ have special meaning in mysql?
Because the underscore matches any single character
The request must escape the underscore to work properly.
Select * from tbl_schoolphotos where Filename like
'glasshouse\_1\_%' order by DateTaken
Info: The LIKE and NOT LIKE have two search helper symobls. The underscore _ character that looks for one character and the percentage % character that looks for zero or more characters.
And you don't want the underscore to be a search helper, you have to escape it with the \ like \_.
Yes, _ in MySQL LIKE matches exactly one character.