how to avoid null condition in declared variable in sqlserver

Viewed 45
CREATE TABLE #UserCompany
    ([ID] int, [CustId] varchar(2), [CustName] varchar(1), [Status] int)
;

INSERT INTO #UserCompany
    ([ID], [CustId], [CustName], [Status])
VALUES
    (1, 'a1', 'A', null),
    (2, 'a1', 'A', null),
    (3, 'a2', 'B', null),
    (4, 'a3', 'B', null),
    (5, 'a4', 'C', null),
    (6, 'a4', 'C', null),
    (7, 'a4', 'D', null),
    (8, 'a6', 'E', null)
;select * from #UserCompany

while passing some value iam getting values

declare @id varchar(100)='1,2'
select * from #UserCompany where id in (select  CAST(value AS int)  FROM string_SPLIT(@id, ','))

if iam passing null iam not getting any value how to retrieve even we dont pass anything as like below

declare @id varchar(100)=null
select * from #UserCompany where id in (select  CAST(value AS int)  FROM string_SPLIT(@id, ','))

output should be total table should display

1 Answers
declare @id varchar(100)='1,2'

select * 
from #UserCompany 
where id in (select  CAST(value AS int)  FROM string_SPLIT(@id, ','))
   OR @id IS NULL;
Related