I have a string that looks like this 'NRT, STH, WST, EST, SCT' stored as a variable parameter to pass to a stored procedure. However I want to use that parameter in an IN clause but obviously that won't work, so I want to split it up so I get this output:
'NRT', 'STH', 'WST', 'EST', 'SCT'
as supposed to the singular string.
I am sure there is a way to do this and after trying to find the solution on here and google I can't find the exact solution to this (sorry if there is one already on SO)
I've got close but I am stuck with the last element having a comma after it.
This is what I have so far
DECLARE @region NVARCHAR(MAX)
SET @region = 'NRT, STH, WST, EST, SCT'
Select *
From
Table
Where
RegionId
In (
Select
Coalesce
(
Case When @region = ''
then Name
Else
Name + ','
End,''
)
From
dbo.splitstring(@region)
)
If I run the Coalesce with the split function on it's own this is the result:
- NRT,
- STH,
- WST,
- EST,
- SCT,
So the IN clause will not work with this. Any Ideas on how to do this?