user3115933
user3115933

Reputation: 4443

How to write this SQL Query with a list of numbers in the WHERE filter

I'm using SQL Server 2012.

My SQL query stands as below:

USE Mydatabase
SELECT TravelagencyId, Name
FROM dbo.TravelAgency

I need to add this WHERE line to the query:

WHERE TravelagencyId = (650,651,652,800,952,976)

I am aware that if the list of numbers are consecutive or fall between a range, I can write something like:

WHERE TravelagencyId BETWEEN 650 AND 976 

but here there are Ids that I want to exclude from the filter.

Any help on how to write this WHERE filter?

Upvotes: 0

Views: 9092

Answers (1)

SMA
SMA

Reputation: 37023

You should be using not in where you want to exclude the values that you have.

USE Mydatabase
SELECT TravelagencyId, Name
FROM dbo.TravelAgency
where TravelagencyId in (650,651,652,800,952,976)

Upvotes: 2

Related Questions