I'm trying to use the string_split function in my where clause to split the values from json filter and create IN clause. The query should return the values found, otherwise return everything.
Filter:
DECLARE @Filter NVARCHAR(MAX)
SET @Filter=N'{
"objectName": "Object2"
}'
The following query, will do exactly what I want, if I only specify one objectName.
...
AND (c.objectName IS NULL AND JSON_VALUE(@Filter,N'$.objectName') IS NULL OR c.objectName= ISNULL(JSON_VALUE(@Filter,N'$.objectName'),c.objectName))
But if I change my Filter to contain , separated values:
SET @Filter=N'{
"objectName": "Object1, Object2"
}'
and use string_split function to read those values, it's not liking the string_split function in the where clause. I believe my syntax is wrong.
AND (c.objectName IS NULL AND JSON_VALUE(@Filter,N'$.objectName') IS NULL OR c.objectName IN
string_split(ISNULL(JSON_VALUE(@Filter,N'$.objectName'),c.objectName), ','))
Does json_value needs to be casted to varchar first?
STRING_SPLITis a table value function. If you want it in theWHEREyou need to include it in a sub query.