This question already has answers here:
Mysql Like containing wildcart giving unexpected result
(2 answers)
Closed 5 years ago.
I have a MySQL table and I need to retrieve some fields from it.
I need the string that contains H_ and HP_ Separately using two different query.
But the problem is the underscore is in the wildcard and i am unable to find correct result.
Example Query of HP_
SELECT nc.`logname`, nc.`client_id`, nc.`Customer_Name`, cb.`bwm_day_cir_upload`, cb.`bwm_day_cir_download`
FROM `new_client` nc
LEFT JOIN `client_bandwidth` cb ON cb.`client_id` = `nc`.`client_id`
WHERE nc.`logname` LIKE '%HP_%' AND nc.`Active` = 'y' ORDER BY TRIM(UPPER(nc.`logname`))
As You told it's a wildcard. Then just escape it with '\_'
SELECT nc.`logname`, nc.`client_id`, nc.`Customer_Name`, cb.`bwm_day_cir_upload`, cb.`bwm_day_cir_download`
FROM `new_client` nc
LEFT JOIN `client_bandwidth` cb ON cb.`client_id` = `nc`.`client_id`
WHERE nc.`logname` LIKE '%HP\_%' AND nc.`Active` = 'y' ORDER BY TRIM(UPPER(nc.`logname`))
Documentation to the rescue! Did you read the following page: https://dev.mysql.com/doc/refman/5.7/en/string-comparison-functions.html#operator_like
Here's an excerpt:
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.
Related
This question already has answers here:
How should I escape characters inside this LIKE query?
(4 answers)
Closed last year.
I have a column called \'column. I am able to use it in SELECT statements just like any other column. However, when I try to perform SHOW COLUMNS FROM myTable LIKE '\\''column' I get no results. I observed that it works if I double escape the backslash: '\\\\''column'.
I tested this from MariaDB console, but I also observed the same behaviour in MySQL 8.
How does the escaping work? How should I properly escape the value so that I can fetch the column information?
DB Fiddle
From the mysql documentation
MySQL uses C escape syntax in strings (for example, \n to represent
the newline character). If you want a LIKE string to contain a literal
, you must double it. (Unless the NO_BACKSLASH_ESCAPES SQL mode is
enabled, in which case no escape character is used.) 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.
This question already has an answer here:
How to match a fixed string ending with a number using regex [duplicate]
(1 answer)
Closed 2 years ago.
I'm trying to select, from mysql, some specific tables that matches a pattern.
The specific pattern I'm looking for is like 'my_table_number'. For example, my_table_436814 or my_table_35413. The thing is that there are other tables that looks like my_table_old_14353 or my_table_351434_times. I just want to filter out the ones that are my_table_number.
I have tried different patterns, but failed to get what I really need.
The most closest approach was with this:
select table_name from
information_schema.columns ca
where ca.table_name REGEXP '[0-9]$'
How can I achieve what I'm looking for?
Use
REGEXP '^my_table_[0-9]+$'
See proof
NODE
EXPLANATION
^
the beginning of the string
my_table_
'my_table_'
[0-9]+
any character of: '0' to '9' (1 or more times (matching the most amount possible))
$
before an optional \n, and the end of the string
This question already has answers here:
MySQL Query, remove all spaces
(2 answers)
Closed 9 years ago.
I have tried using the TRIM() in MySQL to delete all of the whitespace in every field in a column but it will only remove it from the front. I have long strings that have spaces in the middle next to hyphens and I need the space between the hyphens removed. Is there a way to do this?
UPDATE FOO set FIELD2 = TRIM(FIELD2);
Use the REPLACE() method, which can replace every occurrence of a character by another:
UPDATE foo SET field2 = REPLACE(field2, ' ', '');
This query will remove every spaces in the field2 column.
Definition of the method, from the MySQL doc:
Returns the string str with all occurrences of the string *from_str* replaced by the string *to_str*. REPLACE() performs a case-sensitive match when searching for *from_str*.
mysql> SELECT REPLACE('www.mysql.com', 'w', 'Ww');
-> 'WwWwWw.mysql.com'
This function is multi-byte safe.
This question already has answers here:
MySQL variable format for a "NOT IN" list of values
(3 answers)
Closed 8 years ago.
In filtering out some spam, I have two MYSQL statements in one file,
SET #valid_users := '"admin", "jrock", "kmicka", "First Last"'; //etc
Followed by a SELECT like this
SELECT /*stuff*/ /*WHERE*/ /*filters*/ AND User.user_name NOT IN (#valid_users)
When I do this, it acts as if #valid_users is an empty string. (Returns all results). But if I change the clause to NOT IN ('admin', 'jrock', etc) then it works as it should.
Why would a variable in the NOT IN filter not work?
You'll want to take a look at MySQL's find_in_set() function:
SELECT
*
FROM
your_table
WHERE
NOT FIND_IN_SET(User.user_name, #valid_users);
For this to work, the comma-separated list shouldn't contain quotes (unless your usernames actually contain quotes) and should not be padded with spaces:
SET #valid_users := 'admin,jrock,kmicka,First Last';
SqlFiddle Example
To directly answer your question regarding "why would a variable in the NOT IN filter work", it's because #valid_users is being treated as a string and when you pass it to IN(), it's being treated as a single string (i.e. not a set/list). With FIND_IN_SET(), it treats the string in #valid_users as a comma-separated set/list and uses it accordingly.
This question already has answers here:
How can I find non-ASCII characters in MySQL?
(10 answers)
Closed 9 years ago.
I'm trying to find all non-asci characters I have in my DB in a specific table and column. In that column are stored Html description, and in some of them I've exotic or non-existing characters (for example: Hà¶ganà¤s ).
I'm triyng to match them with this query:
SELECT * FROM project_version WHERE description REGEXP '[^()\x00-\xFF\,\.-\<\>="\' /:;&=]'
But I think I'm missing something, cause it returns all of my records. Does anyone any advice?
Thanks in advance
Try moving hyphen to start or end otherwise it needs to be escaped also ^ will be treated as literal ^ in character class:
SELECT * FROM project_version WHERE description REGEXP '[()\x00-\x7F,.<>="\' /:;&=-]'