tadm123
tadm123

Reputation: 8787

SQLite3 - How to select a specific number from a string

I have a table named shoes:

Shoe_model     price    sizes
-----------------------------------------------
Adidas KD      $55      '8, 9, 10, 10.5, 11, 11.5'

Nike Tempo     $56      '8, 9, 11.5'

Adidas XL      $30.99   '9, 10, 11, 13'

How can I select a row for a specific size?

My attempt:

SELECT * FROM shoes WHERE sizes = '10';

Is there a way around this? Using LIKE will get you both 10 and 10.5 sizes so I'm trying to use WHERE for this. Thanks

Upvotes: 0

Views: 134

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269703

First, you should not be storing the sizes as a comma delimited string. You should have a separate row with one row per size per show. That is the SQLish way to store things.

Sometimes, we are stuck with other people's really bad design decisions. In you can do something like this:

SELECT *
FROM shoes
WHERE ',' || '10' || ',' LIKE '%,' || sizes || ',%';

The delimiters are important so "10" doesn't match "100".

Upvotes: 1

Related Questions