I know the meaning of % and _ wildcard characters ,but i was stuck in a question which was using the two additional characters \% and \\,i was not able to understand what these characters actually mean in the SQL query
SELECT productID
FROM productList
WHERE productName LIKE 'ab\%cd%'
and
SELECT productID
FROM productList
WHERE productName LIKE 'ab\\cd%'
are these two same things or different ??
Since % is a special character, you have to escape it with a \ to match a literal % symbol in your data. So, 'ab\%cd%' matches the letter a, followed by the letter b, followed by a % symbol, the letter c, the letter d, then any other text (because the last % is a wildcard).
Similarly, since \ is a special character used to create escape sequences, you have to escape it to match a literal \ in a pattern, so to match a single \ you have to encode it as \\.
I believe the best way to see the difference is by example.
To better understand it you will need knowledge about 3 things when using LIKE operator in SQL:
\ is used to escape special characters to use them as normal chars
% is used to match any number of characters (including 0)
special characters are \ and % so if you want to include them literally you need to escape them, so to check for them in text column you respectively need to use \\ and \%.
Below is a table with words and true/false results for LIKE comparison with both patterns:
word | ab\%cd% | ab\\cd%
----------+---------+---------
ab\ | f | f -- this would match second pattern but there is no "cd" at the end
ab\cd | f | t -- \\ is escaped "\", and % matches none characters
ab\cdxzy | f | t -- \\ is escaped "\", and % matches character sequence "xzy"
abcd | f | f -- every string requires either "%" or "\" character after "ab"
ab%cd | t | f -- \% is escaped "%", and % matches none characters
ab%cdxzy | t | f -- \% is escaped "%", and % matches character sequence "xzy"
ab\%cd | f | f -- there is no pattern which matches both chars "\%" in sequence
ab%\cd | f | f -- same as above, but characters are "%\" in sequence
The \% and \_ sequences are used to search for literal instances
of % and _ in pattern-matching contexts where they would otherwise
be interpreted as wildcard characters.
For \\ it searches for a single back slash \.
Ref: MySQL 8.0 Reference Manual, 9.1.1 String Literals, Table 9.1 Special Character Escape Sequences
Related
It does not work in MySQL (8.0.5+) using ICU-REGEXP to perform a search on the word boundary.
As far as I understand it should be a-la
$ mysql -e 'SELECT REGEXP_LIKE("aaa abc ccc", ".*\b+abc\b+.*")'
+---------------------------------------------+
| REGEXP_LIKE("aaa abc ccc", ".*\b+abc\b+.*") |
+---------------------------------------------+
| 0 |
+---------------------------------------------+
but this option does not work.
First, note that REGEXP_REPLACE can match strings partially, and you do not need .* before and after a search word.
The \ char should be escaped in order to define a literal backslash, since \ itself allows escaping characters for the MySQL engine. See this MySQL 8 documentation:
Note
Because MySQL uses the C escape syntax in strings (for example, \n to represent the newline character), you must double any \ that you use in your expr and pat arguments.
Thus, you need
REGEXP_LIKE("aaa abc ccc", "\\babc\\b")
It does not work in MySQL (8.0.5+) using ICU-REGEXP to perform a search on the word boundary.
As far as I understand it should be a-la
$ mysql -e 'SELECT REGEXP_LIKE("aaa abc ccc", ".*\b+abc\b+.*")'
+---------------------------------------------+
| REGEXP_LIKE("aaa abc ccc", ".*\b+abc\b+.*") |
+---------------------------------------------+
| 0 |
+---------------------------------------------+
but this option does not work.
First, note that REGEXP_REPLACE can match strings partially, and you do not need .* before and after a search word.
The \ char should be escaped in order to define a literal backslash, since \ itself allows escaping characters for the MySQL engine. See this MySQL 8 documentation:
Note
Because MySQL uses the C escape syntax in strings (for example, \n to represent the newline character), you must double any \ that you use in your expr and pat arguments.
Thus, you need
REGEXP_LIKE("aaa abc ccc", "\\babc\\b")
It does not work in MySQL (8.0.5+) using ICU-REGEXP to perform a search on the word boundary.
As far as I understand it should be a-la
$ mysql -e 'SELECT REGEXP_LIKE("aaa abc ccc", ".*\b+abc\b+.*")'
+---------------------------------------------+
| REGEXP_LIKE("aaa abc ccc", ".*\b+abc\b+.*") |
+---------------------------------------------+
| 0 |
+---------------------------------------------+
but this option does not work.
First, note that REGEXP_REPLACE can match strings partially, and you do not need .* before and after a search word.
The \ char should be escaped in order to define a literal backslash, since \ itself allows escaping characters for the MySQL engine. See this MySQL 8 documentation:
Note
Because MySQL uses the C escape syntax in strings (for example, \n to represent the newline character), you must double any \ that you use in your expr and pat arguments.
Thus, you need
REGEXP_LIKE("aaa abc ccc", "\\babc\\b")
It does not work in MySQL (8.0.5+) using ICU-REGEXP to perform a search on the word boundary.
As far as I understand it should be a-la
$ mysql -e 'SELECT REGEXP_LIKE("aaa abc ccc", ".*\b+abc\b+.*")'
+---------------------------------------------+
| REGEXP_LIKE("aaa abc ccc", ".*\b+abc\b+.*") |
+---------------------------------------------+
| 0 |
+---------------------------------------------+
but this option does not work.
First, note that REGEXP_REPLACE can match strings partially, and you do not need .* before and after a search word.
The \ char should be escaped in order to define a literal backslash, since \ itself allows escaping characters for the MySQL engine. See this MySQL 8 documentation:
Note
Because MySQL uses the C escape syntax in strings (for example, \n to represent the newline character), you must double any \ that you use in your expr and pat arguments.
Thus, you need
REGEXP_LIKE("aaa abc ccc", "\\babc\\b")
I want to update column 1 in table A in a specific way: When the third character in the column is an "_" I want to insert the first 2characters, if the third character anythin else I want to leave it as it is.
Example:
|col1|
+--------+
|161_512 |
|16_1217 |
|161_512 |
|161512 |
|17_0117 |
|1615_12 |
Expected outcome:
|col1|
+--------+
|161_512 |
|16 |
|161_512 |
|161512 |
|17 |
|1615_12 |
Thats what I´ve got so far - but this is not working correctly:
UPDATE table A SET col1 = CASE WHEN col1 LIKE '%_%' THEN ... ELSE col1;
If you want to match a literal underscore in your LIKE expression, you will need to escape it using backslash. An unescaped underscore means match any single character. However, I would reword your query such that it uses a WHERE clause to determine whether or not to update a given record.
UPDATE table A
SET col1 = ...
WHERE col1 LIKE '__\_%'
Note carefully here that LIKE __\_% says to match any two characters, followed by a literal underscore, followed by anything else.
If you don't feel comfortable dealing with all this, you can always use a substring to check the value of the third character:
UPDATE table A
SET col1 = ...
WHERE SUBSTRING(col1, 3, 1) = '_'
When the third character in the column is an "_" I want to insert the first 2characters
I don't know what this means. First 2 characters from which attribute? Where do you want to insert them?
When the third character in the column is an "_"
That's simple. As you may already know, the underscore character is a wildcard matching exactly one character. Hence
LIKE '%_%'
will match any attribute with a at least one character. If you explicitly want to match an underscore character then you would escape the underscore
LIKE '%\_%'
And to require this to be the third character, perform a match on 2 single characters before it:
LIKE '__\_%'
(L I K E ' _ _ \ _ ')