简体   繁体   English

将具有多个分隔符的字符串拆分为3个部分

[英]Split a string with multiple delimiters into 3 parts

I have a string read in from a xml file. 我从xml文件中读取了一个字符串。 I need to split it into 3 parts. 我需要将它分成3部分。 I need to run this query in a select statement for an insert query. 我需要在select语句中为插入查询运行此查询。 UPDATE I am suppose to use this in a select query for a insert statement. 更新我想在insert语句的select查询中使用它。

Insert to table1 (col1,col2,company,station,location,coln) select (here i want this query for each columns. ) 插入到table1(col1,col2,company,station,location,coln)select(这里我希望每个列都有这个查询。)

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. 分隔符是4个字符,它总是相同的。

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. 我尝试使用charindex substring ,但我只能获得第一和第二个字符串,但我无法准确获取位置字符串。 Any help appreciated TIA. 任何帮助赞赏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 您可以尝试使用REVERSE()来获取如下所示的Location

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 |
+-------+------------+--------------+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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