• arshad7887 (8/20/2011)


    To Run this kind of query first you need to create UDF(user defined function)

    SPLIT Varchar in sql server

    User Defined Method

    CREATE FUNCTION SplitString

    (

    -- Add the parameters for the function here

    @myString varchar(500),

    @deliminator varchar(10)

    )

    RETURNS

    @ReturnTable TABLE

    (

    -- Add the column definitions for the TABLE variable here

    [id] [int] IDENTITY(1,1) NOT NULL,

    [part] [varchar](50) NULL

    )

    AS

    BEGIN

    Declare @iSpaces int

    Declare @part varchar(50)

    --initialize spaces

    Select @iSpaces = charindex(@deliminator,@myString,0)

    While @iSpaces > 0

    Begin

    Select @part = substring(@myString,0,charindex(@deliminator,@myString,0))

    Insert Into @ReturnTable(part)

    Select @part

    Select @myString = substring(@mystring,charindex(@deliminator,@myString,0)+ len(@deliminator),len(@myString) - charindex(' ',@myString,0))

    Select @iSpaces = charindex(@deliminator,@myString,0)

    end

    If len(@myString) > 0

    Insert Into @ReturnTable

    Select @myString

    RETURN

    END

    GO

    Now Run this query

    RUN The query

    select * From SplitString('Mohammed**Arshad**Shaikh','**')

    You may use it in where clause also.

    Declare @Paramlist varchar(50)

    Set @Paramlist = '1,2,3'

    SELECT * FROM Customer

    WHERE CUSTOMERId In(select * From SplitString(@paramlist,','))

    This will work sure.

    Insha Allah..

    🙂

    No... don't use a While Loop to split strings. They're just too slow. Please see the following article for the code that proves it.

    http://www.sqlservercentral.com/articles/Tally+Table/72993/

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)