1

I have a stored procedure where I will use @variable in

WHERE colname LIKE(@variable) 

This @variable could be a string list representing column names, such as

EXEC usp_cols 'col1, col2, col3';

I want to split this string to list of strings in the stored procedure, so that it converts to 'col1', 'col2', 'col3' in the stored procedure to use it in the LIKE statement:

WHERE colname LIKE('col1', 'col2', 'col3')
3
  • 2
    In SQL 2016 and later versions you could use STRING_SPLIT. Commented Jan 4, 2021 at 17:37
  • 1
    How about a table variable or a table-valued parameter Commented Jan 4, 2021 at 17:50
  • What exactly are you trying to do? What query would you do with the values? Commented Jan 5, 2021 at 7:33

4 Answers 4

2

As mention in comment, from 2016 you can use STRING_SPLIT. Read more about it here: https://learn.microsoft.com/en-us/sql/t-sql/functions/string-split-transact-sql?view=sql-server-ver16

It is as simple to use as this:

SELECT value FROM STRING_SPLIT('Lorem,ipsum,dolor,sit,amet.', ',');
Sign up to request clarification or add additional context in comments.

Comments

1

Could you please try

DECLARE @VARIABLE VARCHAR(100);
SET @VARIABLE='COL1,COL2,COL3';
DECLARE @DELIMITER CHAR(1)=',';
SELECT VALUE FROM STRING_SPLIT(@VARIABLE,@DELIMITER);

--EXAMPLE
IF OBJECT_ID(N'tempdb..#T',N'U')IS NOT NULL
DROP TABLE tempdb..#T;
CREATE TABLE #T
(
   ID TINYINT NOT NULL,
   SURNAME VARCHAR(100)
)
INSERT #T(ID,SURNAME)VALUES(1,'IVAN'),(2,'JOHN'),(3,'PETER'),(4,'PAUL')

DECLARE @InSearchValues VARCHAR(100);
DECLARE @InDelimeter CHAR(1);

SET @InDelimeter=',';
SET @InSearchValues='PETER,PAUL,IV%';


SELECT T.ID,T.SURNAME
FROM #T AS T
CROSS APPLY
 (
    SELECT X.value FROM string_split(@InSearchValues,@InDelimeter)AS X
    WHERE T.SURNAME LIKE X.value
 )R

DROP TABLE #T; 

3 Comments

Consider improving your answer with an example that shows WHERE colname LIKE to answer the question.
What if I have space after ', '?
Also, I need it not as a separate rows but as a list of strings, so I can ass it to the WHERE... LIKE ('col1', 'col2', 'col3') clause
0

Simplest version.

SQL

DECLARE @input VARCHAR(100) = 'COL1  , COL2   , COL3'
    , @separator CHAR(2) = ', '
    , @quote CHAR(1) = CHAR(39)
    , @output VARCHAR(500);

-- Method #1
SET @output = @quote + REPLACE(@input,  @separator, CONCAT(@quote,',', @quote)) + @quote;

SELECT  @output;

/*
Separator could be ',' or ', ' (with space after the coma), ' , ' (or space before and after)
*/
-- Method #2, SQL Server 2017 onwards
SELECT @output = STRING_AGG(CONCAT(@quote, TRIM(value), @quote), ',')
FROM STRING_SPLIT(@input, ',');

SELECT @output;

2 Comments

Separator could be ',' or ', ' (with space after the coma), ' , ' (or space before and after). I cannot guarantee that the person using the stored procedure will enter the data in the format I want. So I need to make sure there is no space in column names.
@Yana, I updated the answer with Method #2. Check it out.
0

With a little help from the community, I think I have the result that I wanted:

DECLARE @VARIABLE VARCHAR(100);
SET @VARIABLE='COL1, COL2, COL3';

DECLARE @DELIMITER CHAR(1)=',';
DECLARE @variable2 varchar(500)

SELECT VALUE
INTO #columnNames
FROM STRING_SPLIT(@VARIABLE,@DELIMITER)

SELECT@variable2 = coalesce(@variable2 + ',', '') + convert(varchar(500), '''' + LTRIM(RTRIM(value)) + '''') from #columnNames

PRINT@variable2

Now I get the following result which can be implemented in WHERE... LIKE clause:

'COL1','COL2','COL3'

5 Comments

What would @variable2 do in this case? You would only have a single result from the temp table
The result from the temp table is a column, I need it in the format that I show above. This is why I use @variable.
Sorry, thought you wanted to split the values to use in a join, not re-aggregate them. Any case, that style of code (aggregating into a variable) is unsafe in the face of parallelism and should not be used to aggregate. Use STRING_AGG instead.
Could you please write the code using STRING_AGG because I am not confident I can use it properly?
@Yana, I updated the answer with Method #2. Check it out.

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.