0

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?

1
  • Shouldn't you use just normal split function without select or coalesce, in cross apply? Commented May 24, 2016 at 16:06

2 Answers 2

1

Create a function as shown below

create  FUNCTION [dbo].[FunctionStringSplit](@stringname NVARCHAR(MAX),@separator nvarchar(1))
RETURNS TABLE
AS
RETURN
    WITH cte AS(
        SELECT CAST(0 AS BIGINT) AS FirstIndex,CHARINDEX(@separator,@stringname) charposition
        UNION ALL
        SELECT charposition+1,CHARINDEX(@separator,@stringname,charposition+1)
        FROM cte
        WHERE charposition>0
    )
    SELECT SUBSTRING(@stringname,FirstIndex,COALESCE(NULLIF(charposition,0),LEN(@stringname)+1)-FirstIndex) AS splitstring
    FROM cte

Then call the function in your select statement like this

 Select *
From
    Table
Where 
    RegionId IN
(
SELECT
  splitstring
FROM [dbo].[FunctionStringSplit] (@Region,',')
)
Sign up to request clarification or add additional context in comments.

Comments

1

I hope your splitstring function convert CSV values into Rows. Please see below function.

CREATE FUNCTION [dbo].[SplitString] 
( 
    @string NVARCHAR(MAX), 
    @delimiter CHAR(1) 
) 
RETURNS @output TABLE(splitdata NVARCHAR(MAX) 
) 
BEGIN 
    DECLARE @start INT, @end INT 
    SELECT @start = 1, @end = CHARINDEX(@delimiter, @string) 
    WHILE @start < LEN(@string) + 1 BEGIN 
        IF @end = 0  
            SET @end = LEN(@string) + 1

        INSERT INTO @output (splitdata)  
        VALUES(SUBSTRING(@string, @start, @end - @start)) 
        SET @start = @end + 1 
        SET @end = CHARINDEX(@delimiter, @string, @start)

    END 
    RETURN 
END

Then you can simply use below code to check for multiple values passed to stored proc.

Select *
From
    Table
Where 
    RegionId IN
(
SELECT
  splitdata
FROM [dbo].[SplitString] (@Region,',')
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.