Sql-server – Can’t get the SQL query to filter by dates like I want

formatsql server

I have a query where I want to exclude rows if a date column has any one of a set of dates.

I am having trouble figuring how to return the correct result sets without filtering one month at a time.

My initial attempts assumed the operator '!=' would be able to specify multiple variables, but I might be mistaken.

How do I filter the results in such a way that removes all the specified dates I do not want?

SELECT * INTO IPS_TransactionDataAll2
FROM
[IPS_TransactionDataAll1]
where Format([Start Date], 'MM/yyyy') != ('07/2016''08/2016''09/2016''10/2016''11/2016''10/2016''01/2017''02/2017''03/2017''04/2017''05/2017''06/2017''07/2017')

Pseudo-code:

Select into TableB
from TableA
Where dates not in the months 7-11/2016 and 1-7/2017

Best Answer

!= doesn't support a list of items like that.

Try one of these (note the commas between items):

WHERE Format([Start Date], 'MM/yyyy') NOT IN ('07/2016','08/2016','09/2016',...)

or even better (and faster), without converting to text (if your date range is truly consecutive like your example):

WHERE [START Date] NOT BETWEEN '2016-07-01' AND '2017-07-31 23:59:59'

Since between is inclusive of the endpoints, that is equivalent to:

WHERE [START Date] >= '2016-07-01' 
AND   [START Date] <  '2017-08-01'