Split 

Posted by Philip Leitch Thursday, June 04, 2009 8:53:46 PM

IF EXISTS (SELECT 'x' FROM   sysobjects WHERE name = 'Split')

            DROP FUNCTION Split
GO
CREATE FUNCTION Split
            (@String as varchar(4000),
            @Delimiter as varchar(4000),
            @Index as int)
RETURNS varchar(4000)
AS
BEGIN
/*
Author: Philip Leitch
Date: 2001
Purpose: Creates an ability to split a varchar (string) into an array and access specific elements of that array.
Copyright: Philip Leitch 2001
Licensing: This code may be used or modified but if the code is included in a software package attribution to me must be made.
Liability: The developer assumes all liability when using this code.
 
Notes:
To locate a specific section out of a string we have to get a start and a length.
The start will be gained by itterate through the string @Index number of times.
The length will be gained by determining the number of characters to the next @Delimiter,
or to the end of the string, whichever is first.
*/
Declare @FoundIndex as int
declare @Length as int
declare @Itteration as int
declare @RemainingString as varchar(4000)
Declare @ReturnString as varchar(4000)
 
Set @Itteration = 0
set @FoundIndex = 0
 
set @RemainingString = @string
 
 
while @Itteration <= @Index+1
            begin
            set @FoundIndex = CHARINDEX (@Delimiter, @RemainingString )
            if @FoundIndex = 0
                        begin
                        --This means that there are no more @Delimiter strings left - so if the
                        --@Index is met we return what we have, otherwise we return nothing (@Index does not exist)
                        if @Index = @Itteration
                                    set @ReturnString = @RemainingString
                        else
                                    set @ReturnString = ''
                        break
                        end
            else
                        begin
                        --This means that we found a @Dilimiter within the remaining string
                        --If we are not at the correct index then we shorten the remaining string.
 
 
 
                        if @Itteration = @Index
                                    begin
                                    --We are at the correct @Index - so now we need to find a length. 
                                    set @FoundIndex = CHARINDEX (@Delimiter, @RemainingString )
                                    if @FoundIndex > 0
                                                begin
                                    --There is at least another delimiter so we need to remove everything after that one
                                                            set @ReturnString = left(@RemainingString, @FoundIndex -1)
                                                end
                                    end
                        else
                                    set @RemainingString = SUBSTRING ( @RemainingString , @FoundIndex+len(@delimiter + '!')-1 , len(@RemainingString) - @FoundIndex )
                        end
            set @Itteration = @Itteration +1
            end
 
            RETURN @ReturnString
End
GO
 
-- =============================================
-- Example to execute function
-- =============================================
SELECT dbo.Split('Hello, this is a test, to see if the split works, which I am sure it does, and if it does you will see a message saying, This function is working normally, okay?', ', ',5)
 


Copyright 2009 Philip Leitch

<a href="http://www.insurslist.com/">carinsurance</a> 52068 <a href="http://www.findinsuronline.com

Friday, April 29, 2011 11:16:45 PM zeepwhat

<a href="http://www.medicplace.net/">Cialis</a> cxf <a href="http://www.mymedicstores.com/">levitra

Friday, April 29, 2011 6:07:06 AM HuyNguyen
Cialis cxf levitra order 513871

<a href="http://www.insurersguidesite.com/">affordable auto insurance</a> 329 <a href="http://www.i

Wednesday, April 27, 2011 7:14:43 AM ruthykiwi

<a href="http://www.megameds.net/">Levitra</a> >:-D <a href="http://www.medicplace.net/">order cial

Monday, April 25, 2011 6:08:51 PM billybrison

<a href="http://www.insursafeco.com/">auto insurance quotes</a> lbvhij <a href="http://www.insursea

Saturday, April 23, 2011 11:15:35 PM Ichirakuu

<a href="http://www.polymeds.net/">are ultram pain pills addictive</a> 6300 <a href="http://www.med

Saturday, April 23, 2011 4:07:59 AM xXTCWXxEVI

N4q4vZ <a href="http://ivcbytaylbhw.com/">ivcbytaylbhw</a>, [url=http://sehcuqjyxknk.com/]sehcuqjyx

Friday, April 01, 2011 6:46:36 PM zdlgxpsq
N4q4vZ ivcbytaylbhw, [url=http://sehcuqjyxknk.com/]sehcuqjyxknk[/url], [link=http://uwkaovztexiy.com/]uwkaovztexiy[/link], http://feucfjdaxjid.com/