Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2004
    Posts
    49

    Unanswered: String tokenizing..

    Is there any function that tokenizing a string?

    any similar function that works like Split in VB?

    for example @inputParam = 'test1,test2,test3'
    select fnSomeFunc(@inputParam, ',')
    returns
    -----------
    test1
    test2
    test3..
    MCSD .NET, SCJP, SCJWD

  2. #2
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Give this a shot:
    Code:
    --  ptp  20040507  Split a string on a separator, like VB
    
    CREATE FUNCTION dbo.fSplit(
       @pcString	VARCHAR(8000)
    ,  @pcSep	VARCHAR(8000) = ','
       ) RETURNS @r TABLE (
       item		VARCHAR(8000)
       ) AS
    BEGIN
    
       DECLARE @i		INT
    
       SET @i = CharIndex(@pcSep, @pcString)
    
       WHILE 0 <> @i
          BEGIN
             INSERT INTO @r (item) SELECT Left(@pcString, @i - 1)
             SET @pcString = SubString(@pcString, @i + Len(@pcSep), 8000)
             SET @i = CharIndex(@pcSep, @pcString)
          END
    
       INSERT INTO @r (item) SELECT @pcString
    
       RETURN
    END
    GO
    
    SELECT item FROM dbo.fSplit('This,is,a,test', ',')
    -PatP

  3. #3
    Join Date
    Apr 2004
    Posts
    49
    Thank you pat, it works for me!!
    MCSD .NET, SCJP, SCJWD

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •