Avatar of Roebbelen
Roebbelen

asked on 

TSQL Looping thru a table spliting strings

I have a table that I want to split out a string

Geocode, SFS
0354, John/Joe/Bill
0403, Sky
6795, Jason/Bill

I want to separate the names into separtate rows
0354, John
0354, Joe
0354, Bill
0403, Sky
6795, Jason
6795, Bill

I have a split Function that splits strings, however I'm struggling with how to loop through the table and join it back up with the Geocode...

CREATE FUNCTION dbo.Split3 ( @strString varchar(4000)) 
RETURNS  @Result TABLE(Value BIGINT) 
AS 
BEGIN 
     DECLARE @x XML  
	   SELECT @x = CAST('<A>'+ REPLACE(@strString,'/','</A><A>')+ '</A>' AS XML) 
       INSERT INTO @Result             
      
	  SELECT t.value('.', 'int') AS inVal 
      FROM @x.nodes('/A') AS x(t) 
    RETURN 
END    
GO   

Open in new window


The following Sample Code  is what I want using a cross apply to other existing tables howerver currently its not working...

 select [GEOMapCode],SFS from [dbo].[MapData]

;with cte as (SELECT GeoMapCode, dbo.Split3('.'/ 'VARCHAR(200)') AS series
FROM (SELECT [GEOMapCode], CAST ('<M>' + REPLACE(series, '/', '</M><M>') + '</M>' AS XML) AS String
	FROM  dbo.MAPDATA) AS A
CROSS APPLY String.nodes ('/M') AS Split(a))
select row_number() over(order by [GEOMapCode], series) as id, [GEOMapCode], series
from cte
ORDER BY 1,2

Open in new window

Microsoft SQL ServerMicrosoft SQL Server 2008

Avatar of undefined
Last Comment
Roebbelen
ASKER CERTIFIED SOLUTION
Avatar of Louis01
Louis01
Flag of South Africa image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Roebbelen
Roebbelen

ASKER

Thanks I assume the Suff is my String spit function?
Avatar of Roebbelen
Roebbelen

ASKER

--Do the work
with tmp(GeoMapCode, DataItem, Data) as (
    select GeoMapCode
         , LEFT(SFS, CHARINDEX('/',SFS+'/')-1)
         , STUFF(SFS, 1, CHARINDEX('/',SFS+'/'), '')
    from dbo.MapData t1
    union all
    select GeoMapCode
         , LEFT(Data, CHARINDEX('/',Data+'/')-1)
         , STUFF(Data, 1, CHARINDEX('/',Data+'/'), '')
    from tmp
    where Data > ''
)
select GeoMapCode, DataItem
  from tmp
 order by GeoMapCode 

Open in new window


I get the error...

Level 16, State 1, Line 2
Types don't match between the anchor and the recursive part in column "DataItem" of recursive query "tmp".
Avatar of Roebbelen
Roebbelen

ASKER

Thank you

Ok I got it.. It worked with the following. I realized I had some duplicate data in my table.
Thanks a million


--Prepare the data
declare @IhaveAtable table (GeoMapCode nvarchar(255), SFS nvarchar(max));
INSERT INTO @IhaveAtable (GeoMapCode, SFS) (Select GeoMapCode, SFS FROM dbo.MapData GROUP By GeoMapCode, SFS);
--insert into @IhaveAtable values ('0403', 'Sky');
--insert into @IhaveAtable values ('6795', 'Jason/Bill');

--Do the work


with tmp(GeoMapCode, DataItem, Data) as (
    select GeoMapCode
         , LEFT(SFS, CHARINDEX('/',RTRIM(LTRIM(SFS))+'/')-1)
         , STUFF(SFS, 1, CHARINDEX('/',RTRIM(LTRIM(SFS))+'/'), '')
    from @IhaveAtable t1 WHERE SFS is not null
    union all
    select GeoMapCode
         , LEFT(Data, CHARINDEX('/',RTRIM(LTRIM(Data))+'/')-1)
         , STUFF(Data, 1, CHARINDEX('/',RTRIM(LTRIM(Data))+'/'), '')
    from tmp
    where Data > ''
)
select GeoMapCode, DataItem
  from tmp
 -- Where GeoMapCode='51610'
  Group by GeoMapCode, DataItem
 order by GeoMapCode
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo