Sewder
Sewder

Reputation: 754

How do I pass multiple options for a like statement?

I have this query

Select Model_Number
From Guitar.Fenders
Where Model_number LIKE ('5%' OR '6%')

This doesn't work.

I only want the query to return model numbers that begin with 5 or 6.

Upvotes: 0

Views: 59

Answers (4)

Satwik Nadkarny
Satwik Nadkarny

Reputation: 5135

There are a couple of ways you can do this, one by making use of LIKE and the other by making use of IN :

LIKE

Select       Model_Number
From         Guitar.Fenders
Where        Model_number LIKE '5%' OR Model_Number LIKE '6%'

IN

Select       Model_Number
From         Guitar.Fenders
Where        LEFT(Model_number, 1) IN ('5','6')

I'd prefer to use the IN statement in case the 'conditions' are likely to increase.

If not, you could simplify this by making use of:

Select       Model_Number
From         Guitar.Fenders
WHERE        Model_number >= '5' AND Model_number < '7'

You can choose any of these three which suits your needs.

Hope this helps!!!

Upvotes: 3

AngocA
AngocA

Reputation: 7693

You can define a Regular Expression for this case, something like:

^[56].*$

And then pass the regular expression using Xquery.

Upvotes: 0

MatthewJ
MatthewJ

Reputation: 3187

This is probably the easiest way.

Select Model_Number
From Guitar.Fenders
Where Model_number LIKE '5%' OR Model_number like '6%'

Hope this helps, MatthewJ

Upvotes: 0

mti2935
mti2935

Reputation: 12027

Select Model_Number From Guitar.Fenders Where (Model_number LIKE '5%' OR Model_number LIKE '6%')

Upvotes: 0

Related Questions