将包含多个分隔符的字符串拆分为3个部分

时间:2016-12-14 12:10:10

标签: sql sql-server sql-server-2008 split

我从xml文件中读取了一个字符串。我需要将它分成3部分。我需要在select语句中为插入查询运行此查询。 的更新 我想在insert语句的select查询中使用它。

插入到table1(col1,col2,company,station,location,coln)    选择(这里我希望每个列都有这个查询。)

字符串示例:

@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);

分隔符是4个字符,它始终相同。

例如第一个字符串,我需要拆分并分配

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

例如第二个字符串

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

我用substring尝试了charindex,但我只能获得第一和第二个字符串,但我无法准确获取位置字符串。任何帮助都赞赏TIA。

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

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

我无法正确识别位置和站点。

5 个答案:

答案 0 :(得分:4)

这将按顺序为您提供字符串的三个部分

    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)

但如果你想做变量,你可以去..

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

答案 1 :(得分:2)

  

创建一次函数并使用它一百万次:)

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

使用以下功能:

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. '

答案 2 :(得分:2)

也试试这个。这会将字符串转换为动态字符串,并提供值

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

答案 3 :(得分:0)

您可以尝试使用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;

这给了我以下结果

enter image description here

答案 4 :(得分:0)

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