Handling Delimited Strings in T-SQL

  • Comments posted to this topic are about the item Handling Delimited Strings in T-SQL


    Harinatha Reddy.G
    Smart Software Technologies,
    Hyderabad , India.

  • I used to write similar code like this for years and then discovered tally tables made it easier and faster IMHO.

    Here's an example:

    http://www.sqlservercentral.com/scripts/Miscellaneous/31913/

  • CREATE FUNCTION dbo.udfStringtoTable

    (

    @String NVARCHAR(100) ,

    @Delimiter CHAR(1)

    )

    RETURNS @Results TABLE

    (String VARCHAR(100))

    AS

    BEGIN

    INSERT INTO @Results

    SELECT SUBSTRING(@String+@Delimiter, n,

    CHARINDEX(@Delimiter, @String+@Delimiter, n) - n)

    FROM tally

    WHERE n <= LEN(@String)

    AND SUBSTRING(@Delimiter + @String,

    n, 1) = @Delimiter

    ORDER BY n

    RETURN

    END

    GO

    ----------------------------------------------
    Try to learn something about everything and everything about something. - Thomas Henry Huxley

    :w00t:
    Posting Best Practices[/url]
    Numbers / Tally Tables[/url]

    SQL-4-Life

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply