• UMG Developer (1/5/2011)


    Alberto IT (1/5/2011)


    To complicated. Yo can solve it using CHARINDEX:

    As long as you don't mind incorrect results:

    DECLARE @inputValue NVARCHAR(20)

    DECLARE @student TABLE

    (Id INT PRIMARY KEY IDENTITY(1,1),

    StudentName NVARCHAR(50),

    StudentResult INT)

    INSERT INTO @student

    VALUES( 'Hardy', 100), ('Rocky', 98), ('Panky', 99), ('Ardy', 99)

    --Select 1

    SET @inputValue = 'Hardy'

    SELECT * FROM @student WHERE StudentName IN (@inputValue)

    --Select 2

    SET @inputValue = 'Hardy,Rocky'

    SELECT * FROM @student WHERE CHARINDEX(StudentName, @inputValue) > 0

    Ardy shouldn't be returned but it is.

    Good point! CHARINDEX should work only with short and controled set of data.

    Alberto De Rossi
    Microsoft Certified Solutions Associate - SQL Server
    Microsoft MVP - Data Platform
    Power BI User Group Lima - Community Leader