简体   繁体   中英

Split a string with multiple delimiters into 3 parts

I have a string read in from a xml file. I need to split it into 3 parts. I need to run this query in a select statement for an insert query. UPDATE I am suppose to use this in a select query for a insert statement.

Insert to table1 (col1,col2,company,station,location,coln) select (here i want this query for each columns. )

String examples:

@declare exValue1 nvarchar(100) = 'Tempo > XNX (Marc) > Stores/Parts';
@declare exValue2 nvarchar(100) = 'Sedan 12 > XNX (Peter Inc) > Stores/Inventory'; 
@declare @company varchar(25);
@declare @station varchar(25);
@declare @location varchar(50);

Delimiter is the 4 characters and its always same.

For example 1st string, I need to split and assign

 Tempo to company, XNX (Marc) to station, Stores/Parts to location.

For example 2nd string

Sedan 12 to company, XNX (Peter Inc) to station, Stores/Inventory to location.

I tried substring with charindex but I could only get 1st and 2nd string but I couldn't get the location string exactly. Any help appreciated TIA.

select @company = SUBSTRING(@exValue1, 1, CHARINDEX('>', @test) - 1)

select @station = SUBSTRING (@exValue1, CHARINDEX('>', @test) + 4, LEN(@test))

I can't figure the location and station properly.

This will give you three parts of the string in order

    declare @exValue1 nvarchar(100) = 'Tempo > XNX (Marc) > Stores/Parts';

    SELECT LTRIM(RTRIM(m.n.value('.[1]','varchar(8000)'))) AS slices
    FROM (SELECT CAST('<XMLRoot><RowData>' + REPLACE(@exValue1,'&gt;','</RowData><RowData>') + '</RowData></XMLRoot>' AS XML) AS x)t
    CROSS APPLY x.nodes('/XMLRoot/RowData')m(n)

But if you want to do with variables, here you go..

declare @exValue1 nvarchar(100) = 'Tempo &gt; XNX (Marc) &gt; Stores/Parts';

select  SUBSTRING(@exValue1, 1, CHARINDEX('&gt;', @exValue1) - 1)
set @exValue1 = SUBSTRING(@exValue1,CHARINDEX('&gt;', @exValue1)+4,len(@exValue1))
select  SUBSTRING(@exValue1, 1, CHARINDEX('&gt;', @exValue1) - 1)
set @exValue1 = SUBSTRING(@exValue1,CHARINDEX('&gt;', @exValue1)+4,len(@exValue1))
select  @exValue1

create function once and use it million times :)

CREATE  FUNCTION dbo.splitstring ( @stringToSplit VARCHAR(MAX) )
RETURNS
 @returnList TABLE ([Name] [nvarchar] (500))
AS
BEGIN

 DECLARE @name NVARCHAR(255)
 DECLARE @pos INT

 WHILE CHARINDEX('&gt;', @stringToSplit) > 0
 BEGIN
  SELECT @pos  = CHARINDEX('&gt;', @stringToSplit)  
  SELECT @name = SUBSTRING(@stringToSplit, 1, @pos-1)

  INSERT INTO @returnList 
  SELECT @name

  SELECT @stringToSplit = SUBSTRING(@stringToSplit, @pos+4, LEN(@stringToSplit)-@pos)
 END

 INSERT INTO @returnList
 SELECT @stringToSplit

 RETURN
END

Use the function as:

declare @exValue1 nvarchar(100) = 'Tempo &gt; XNX (Marc) &gt; Stores/Parts';

declare @company varchar(25)
set @company=(select top 1 name from dbo.splitstring(@exValue1))
declare @station varchar(25)
set @station=(select top 1 name from dbo.splitstring(@exValue1) where name not in (select top 1 name from dbo.splitstring(@exValue1)))
declare @location varchar(50)
set @location=(select top 1 name from dbo.splitstring(@exValue1) where name not in (select top 2 name from dbo.splitstring(@exValue1)))



print @company+' to comapny, '+@station+' to station, '+@location+' to location. '

Try this also. This will convert the string to a dynamic one and will give the values

declare @exValue1 nvarchar(100) = 'Tempo &gt; XNX (Marc) &gt; Stores/Parts';
declare @exValue2 nvarchar(100) = 'Sedan 12 &gt; XNX (Peter Inc) &gt; Stores/Inventory'; 
declare @company varchar(25);
declare @station varchar(25);
declare @location varchar(50);
DECLARE @Tbl AS TABLE(company varchar(25), station varchar(25), location varchar(50))

SET @exValue1 = 'SELECT '''+REPLACE(@exValue1,'&gt;',''' , ''')+''''
INSERT INTO @Tbl
EXEC(@exValue1)
SET @exValue2 = 'SELECT '''+REPLACE(@exValue2,'&gt;',''' , ''')+''''
INSERT INTO @Tbl
EXEC(@exValue2)

SELECT * FROM @Tbl

You can try using REVERSE() to get Location like below

declare @exValue1 nvarchar(100) = 'Tempo &gt; XNX (Marc) &gt; Stores/Parts';
declare @exValue2 nvarchar(100) = 'Sedan 12 &gt; XNX (Peter Inc) &gt; Stores/Inventory'; 
declare @company varchar(25);
declare @station varchar(25);
declare @location varchar(50);

select @company= SUBSTRING(@exValue1,1,CHARINDEX('&gt;',@exValue1)-1)
select @location = reverse(SUBSTRING(reverse(@exValue1),1, CHARINDEX(';tg&',reverse(@exValue1))-1))
select @station = REPLACE(REPLACE(REPLACE(@exValue1,@company,''),@location,''),'&gt;','')

select @company company, @station station, @location location;

This gave me below result

在此输入图像描述

declare @exValue1 nvarchar(100) = 'Tempo &gt; XNX (Marc) &gt; Stores/Parts'
       ,@company  varchar(25)
       ,@station  varchar(25)
       ,@location varchar(50)

select  @company  = x.value('(/r/e)[1]','nvarchar(25)') 
       ,@station  = x.value('(/r/e)[2]','nvarchar(25)') 
       ,@location = x.value('(/r/e)[3]','nvarchar(50)') 

from   (select cast ('<r><e>'+replace(@exValue1,'&gt;','</e><e>')+'</e></r>' as xml) as x) x


select  @company,@station,@location

+-------+------------+--------------+
| Tempo | XNX (Marc) | Stores/Parts |
+-------+------------+--------------+

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM