I want to use a parameter as the Values of a NOT IN operation, against a table with a field called DateID; but I can't get it to work. Take the following code as an example:
DECLARE @NewDateIDs as VARCHAR(1000)
SET @NewDateIDs = '66,67,68,69,70'
SELECT * FROM MyTable WHERE UserID = 1 AND EventID = 19 AND
CONVERT(VARCHAR(1000), DateID) NOT IN(@NewDateIDs)
When I run the above select statement, all of the records where UserID = 1 and EventID = 19 are returned. What should be returned is one record where UserId = 1, EventID = 19, and DateID = 65.
However, If I run the following select statement, I get the desired results.
SELECT * FROM MyTable WHERE UserID = 1 AND EventID = 19 AND
CONVERT(VARCHAR(1000), DateID) NOT IN(66,67,68,69,70)
I know I am missing something pretty basic here. Could someone show me how to do this properly and why what I am trying is not working.