Leon
Leon

Reputation: 581

How to use SQL's BETWEEN condition when you're excluding a number in between?

Right now, I'm using the BETWEEN condition to gather up account numbers, but the only account numbers I want to grab are:

2999, 3000 to 3004, and 3006 - 3010 (basically skipping 3005)

How would I format the BETWEEN?

Upvotes: 0

Views: 1962

Answers (1)

Lukasz Szozda
Lukasz Szozda

Reputation: 175596

You can use NOT IN to exclude values from range:

SELECT *
FROM your_table
WHERE col BETWEEN 2999 AND 3010
  AND col NOT IN (3005) -- values you want to exclude

Upvotes: 12

Related Questions