Jmac88
Jmac88

Reputation: 91

selecting rows which contain certain characters

I am working with a product database and i need to select products that are greater than £150 also only entries that have "HP" in the third and forth position of the prod_id.

So far i have tried

SELECT * FROM products WHERE prod_id LIKE 'hp%';

Here is a pic of the table i need to query

enter image description here

Upvotes: 0

Views: 1293

Answers (2)

Multisync
Multisync

Reputation: 8797

SELECT * FROM products WHERE prod_id LIKE '__hp%' and price > 150;

"_" (underscore) is any symbol (there are 2 undrscores in this query)

Upvotes: 2

crthompson
crthompson

Reputation: 15865

An underscore will match a single character.

So:

SELECT * FROM products 
WHERE prod_id LIKE '__hp%'
AND price > 150;

Should find all products whos 3rd and 4th character are hp and have a price of 150 or more

Upvotes: 2

Related Questions