Coma seperated stored proc method

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Paulson
    New Member
    • Apr 2007
    • 63

    Coma seperated stored proc method

    /*************** *************** *************** ***** ***************
    **** Parse A Comma Delimited String Into A Table
    *************** *************** *************** ***** ***************/
    CREATE FUNCTION dbo.ParseByComm a (
    @String VARCHAR(600) )
    RETURNS @TblSubString TABLE
    (
    VarSubString VARCHAR(10)
    )
    AS
    BEGIN
    DECLARE @intPos INT,
    @SubStr VARCHAR(10)

    -- Remove All Spaces
    SET @String = REPLACE(@String , ' ','')
    -- Find The First Comma
    SET @IntPos = CHARINDEX(',', @String)
    -- Loop Until There Is Nothing Left Of @String
    WHILE @IntPos > 0
    BEGIN
    -- Extract The String
    SET @SubStr = SUBSTRING(@Stri ng, 0, @IntPos)
    -- Insert The String Into The Table
    INSERT INTO @TblSubString (VarSubString) VALUES (@SubStr)
    -- Remove The String & Comma Separator From The Original
    SET @String = REPLACE(@String , @SubStr + ',', '')
    -- Get The New Index To The String
    SET @IntPos = CHARINDEX(',', @String)
    END
    -- Return The Last One
    INSERT INTO @TblSubString (VarSubString) VALUES (@String)
    RETURN
    END

    Doing like this takes a very long time.can anyone help me?
  • RodneyAnonymous
    New Member
    • Oct 2007
    • 15

    #2
    If I'm reading this correctly, it sounds like you're trying to take a string that has a bunch of values split by commas (value1,value2, value3,value4).

    Whatever language you're using (since you didn't specify) should have a String.Split function that will take a parameter (in your case, a comma), chop up a string into pieces based on where that paramenter is, and return a String array.

    You'll want to use that.

    Comment

    Working...