I have a SQL function( SplitString
) which returns string value by considering the separator(;) string to split the string. After getting the string one by one, I want to query in my table by using WHERE
AND IN
clause. But I have noticed it is not querying when the value is null. I am trying to figure out how to resolve it. My function( SplitString
) takes 2 parameters. First the string and the separator. If I do the query, it returns empty string as NULL and other value.
SELECT [Value] FROM ACDC.SplitString(N';01e178bd-8f6c-488f-a0fa-81ffb598e569', ';') s
So from the above query it will return NULL
AND 01e178bd-8f6c-488f-a0fa-81ffb598e569
:
Now I want to call a table with this query,
select * from Modeltable where ModelName IN (SELECT
[Value]
FROM ACDC.SplitString(N';01e178bd-8f6c-488f-a0fa-81ffb598e569', ';') s)
In Modeltable
, I have also null value which I want to fetch as well. What I understand that sql IN
operator ignore the NULL value here. In this case, what should be the correct way to get data both for null and other string value?
2条答案
按热度按时间vxqlmq5t1#
You can use an
EXISTS
query and simpleAND
OR
logic.In modern versions of SQL Server, you can use
STRING_SPLIT
andIS NOT DISTINCT FROM
Note that
STRING_SPLIT
returns''
notNULL
for an empty separator section, which is why you need to null it out before you compare it.u5rb5r592#
You have to convert the NULL values on both sides of the expression to something that will compare normally:
Note this breaks index use on the
ModelName
column, so it's better to plan ahead to not need to compare NULL at all when possible.