Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL statement with LIKE

I would like to select all records that have an underscore character in their 11th character, so i try this:

SELECT * FROM "BOM_SUB_LEVEL" where TOP_CODE like '%%%%%%%%%%_%%%'

but this doesnt work as expected, can someone help?

like image 240
chicane Avatar asked Nov 27 '22 12:11

chicane


2 Answers

Just use the "SUBSTRING" function :

SELECT * FROM "BOM_SUB_LEVEL" where SUBSTRING(TOP_CODE, 11, 1) = "_"

Marc

like image 129
marc_s Avatar answered Dec 10 '22 02:12

marc_s


For a single character wildcard use _. For multiple characters wildcards, use %. To escape a "real" appearance of _, use \_ (thanks Bill!).

Try the following code:

SELECT * FROM "BOM_SUB_LEVEL" where TOP_CODE like '___________\_%'

To further elaborate following Dav's comment, note that '%%%' is exactly the same as '%', since by definition '%' covers multiple characters.

like image 33
Roee Adler Avatar answered Dec 10 '22 04:12

Roee Adler