Link to home
Start Free TrialLog in
Avatar of anjana_i
anjana_i

asked on

Split string with delimitters

Hey guys,

I have got a function that will split the string considering the a string delimitted by the delimiters( comma , semicolan). The string might also contain spaces.
The first stage does not take space as Delimitter ... but in the other level we should consider tha space to be adelimitter .. how do we do it...

Ex:   I/p string is 'Best Buy,Dell,Office Max;ATT"

The final output should be

Best Buy
Office Max
Dell
ATT
Best
Buy
Office
Max

Please let me know how to do it...
split.txt
Avatar of Aneesh
Aneesh
Flag of Canada image

create    FUNCTION [dbo].[fn_ParseString](
@OriginID nvarchar(4000))

RETURNS @tbl table (s varchar(1000))
as
/*
SELECT * from dbo.fn_ParseString ('1,2,0,6,7')
*/
BEGIN
      DECLARE @i int ,@j int
      SELECT       @i = 1
      WHILE @i <= len(@OriginID)
      BEGIN
            SELECT      @j = charindex(',', @OriginID, @i)
            if @j = 0
            BEGIN
                  SELECT      @j = len(@OriginID) + 1
            end
            INSERT      @tbl SELECT substring(@OriginID, @i, @j - @i)
            SELECT      @i = @j +1
      END
      RETURN
END

ASKER CERTIFIED SOLUTION
Avatar of Thomasian
Thomasian
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial