Ghanshyam Singh
Ghanshyam Singh

Reputation: 1381

Split comma sepearted from Variable and check if it contains a specific number in Sql server Stored procedure

I have a stored procedure in which i declared a variable and getting multiple values in it (separated with comma)

I want to check if it contains a specific number.If contains then run other queries.

Thanks in Advance :)

Upvotes: 0

Views: 36

Answers (1)

Mazhar
Mazhar

Reputation: 3837

Using Aaron Bertrand's split string function you can do something like this.

DECLARE @CSVString NVARCHAR(MAX) = '13,4325,345,987, 432';
DECLARE @Id_To_Find INT = 4325

;WITH cteIdSplit
AS(
        SELECT
            CAST([Value] AS INT) 'Id'
        FROM
            dbo.FN_SplitString_AB (@CSVString, ',') A
        WHERE
            vn = 1
)
SELECT * FROM cteIdSplit S WHERE S.Id = @Id_To_Find;

Upvotes: 1

Related Questions