if column value is 2_apples
or final_chapter
how do i match a value that contains an underscore "_"
?
I've tried with:
SELECT DISTINCT brand FROM `items` WHERE brand like '%\\_'
SELECT DISTINCT brand FROM `items` WHERE brand like '_'
SELECT DISTINCT brand FROM `items` WHERE brand like '%_%'
SELECT DISTINCT brand FROM `items` WHERE brand like LIKE '\_'
It either shows all results or none. Any suggestions?
If you are searching for an underscore then escape it with a '\' so you would search for' \_'. When matching SQL patterns, you can use these symbols as placeholders: % (percent) to substitute for zero or more characters, or _ (underscore) to substitute for one single character.
STRCMP() function in MySQL is used to compare two strings. If both of the strings are same then it returns 0, if the first argument is smaller than the second according to the defined order it returns -1 and it returns 1 when the second one is smaller the first one.
MySQL provides two wildcard characters for constructing patterns: percentage % and underscore _ . The percentage ( % ) wildcard matches any string of zero or more characters. The underscore ( _ ) wildcard matches any single character.
Try
WHERE brand LIKE '%\_%'
The underscore is a wildcard for a single character whereas the % is a wildcard for multiple (or none) characters. Put a backslash in front to escape them.
If you don't like escaping things, because it's ugly or scary, then an alternative would be to use the REGEXP
operator with underscore directly:
WHERE brand REGEXP '_'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With