The following param below would look something like (2,3,4,5) or (2) or even be NULL the column ServiceEntryPart could have data as below in the code section. My question is what will be the correct syntax so I could use the filter. If the filter is NULL, I will totally ignore it. What I have so far works fine if its a NULL (Excludes the filter) but when I have the param for example (1,2), it does not work.. Thanks
ServiceEntryPart.ServiceTypeIDs
4,3
3
NULL
1
8
2,5
--Filter:
@ServiceTypes nvarchar(100) = NULL
--Filter with values SET @ServiceTypes = (1,2,4,5)
--Where Clause
WHERE (ServiceEntryPart.ServiceTypeIDs = ISNULL(@ServiceTypes,ServiceEntryPart.ServiceTypeIDs)
It looks like you want to pass in a variable list for an IN clause. One way is with dynamic SQL, but I would say that is overkill.
You can try something like:
This is using a string search to do the equivalent of IN. It assumes that @ServiceTypes is a comma delimited list with no spaces. It prepends and postpends a comma. When you look for something like 5, it really looks for “,5,” so it does not match 25 or 55 in the list.