search special character in mysql string fucntions - mysql

I need to use special character like ( \ ) character in mysql string function and unfortunately it doesn't work properly!for example couldn't search this character alone (locate-instr-substring_index-concat and even in set variable value are function that i need and test )
like thses
SELECT LOCATE("\", "Schools.co\m", 1) AS MatchPosition;
select SUBSTRING_INDEX("footba\l","\",1)
I will appreciate if anybody could help me

Backslash needs to be escaped. To fix your SUBSTRING_INDEX example, consider the following:
SELECT SUBSTRING_INDEX("footba\\l","\\",1) FROM dual
Here, backslash has to be escaped both in the string literal and in the text to match.
To escape a literal backslash inside a LIKE expression, use four backslashes, e.g.
SELECT 'match' FROM dual WHERE "footba\\l" LIKE '%\\\\%';
Demo

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.

SQL - How to use wildcard in LIKE as a normal character

if I have a column with some values that starts with "%" like this:
[ID]-----[VALUES]
1--------Amount
2--------Percentage
3--------%Amount
4--------%Percentage
how can I have only these two rows with a "select" query?:
[ID]-----[VALUES]
3--------%Amount
4--------%Percentage
I tried these queries but them don't work:
select * from TABLE where VALUES like '[%]%'
select * from TABLE where VALUES like '\%%'
I know that in Java, C and other languages, the backspace \ let you use a jolly character as a normal one like:
var s = "I called him and he sad: \"Hi, there!\"";
There is a similar character/function that do it in SQL?
All answers will be appreciated, thank you for reading the question!
Your query
select * from TABLE where VALUES like '\%%'
should work. The reason it doesn't is because you may have NO_BACKSLASH_ESCAPES enabled which would treat \ as a literal character.
A way to avoid it is using LIKE BINARY
select * from TABLE where VALUES like binary '%'
or with an escape character (can be any character you choose) specification.
select * from TABLE where VALUES like '~%%' escape '~'
try this :
select * from TABLE where VALUES like '%[%]%'
There is an ESCAPE option on LIKE:
select *
from TABLE
where VALUES like '$%%' escape '$';
Anything following the escape character is treated as a regular character. However, the default is backslash (see here), so the version with backslash should do what you want.
Of course, you could also use a regular expression (although that has no hope of using an index).
Note: escape is part of the answer standard so it should work in any database.
You're right that you'll need an escape character for this. In SQL you have to define the escape character.
SELECT * FROM TABLE where VALUES like ESCAPE '!';
I'm pretty sure you can use whatever character you want.
Here's a link to a microsoft explanation that goes into more detail.
Microsoft explanation
MySQL Explanation

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