2017-08-14 221 views
5

我有一個字符串值,其數值由逗號分隔,然後由管道分隔。我想把它們分成兩列的表格。我可以用一個分隔符分割字符串,但不幸的是無法找到一種方法來分割兩個。請幫忙。由兩個分隔符將字符串拆分爲兩列

DECLARE @list NVARCHAR(MAX) = '1,101|2,202|3,303'; 

結果應該如下。

1 101 
2 202 
3 303 

在此先感謝。

+0

你讀過這[文章](https://stackoverflow.com/a/28109664/1050927)了嗎? – Prisoner

+0

將只有兩列?,我的意思是'1,101' – TheGameiswar

回答

1

如果您使用的是SQL Server 2016或Azure,則可以訪問新的SPLIT_STRING函數。如果沒有,我建議使用傑夫MODEN的DelimitedSplit8K功能,這被廣泛認爲是最快,最有效的SQL基於字符串可分流...

DECLARE @list NVARCHAR(MAX) = '1,101|2,202|3,303'; 

SELECT 
    Col1 = LEFT(dsk.Item, sl.SplitLocation - 1), 
    Col2 = SUBSTRING(dsk.Item, sl.SplitLocation + 1, LEN(dsk.Item)) 
FROM 
    dbo.DelimitedSplit8K(@list, '|') dsk -- code for DelimitedSplit8K can be found here... http://www.sqlservercentral.com/articles/Tally+Table/72993/ 
    CROSS APPLY (VALUES (ISNULL(NULLIF(CHARINDEX(',', dsk.Item, 1), 0), 1))) sl (SplitLocation); 
0
CREATE FUNCTION [dbo].[fn_Split_char](@text nvarchar(max), @delimiter varchar(20) = ' ') 
RETURNS @Strings TABLE 
( 
    position int IDENTITY PRIMARY KEY, 
    value nvarchar(max) 
) 
AS 
BEGIN 

DECLARE @index int 
SET @index = -1 

WHILE (LEN(@text) > 0) 
    BEGIN 
    SET @index = CHARINDEX(@delimiter , @text) 
    IF (@index = 0) AND (LEN(@text) > 0) 
     BEGIN 
     INSERT INTO @Strings VALUES (@text) 
      BREAK 
     END 
    IF (@index > 1) 
     BEGIN 
     INSERT INTO @Strings VALUES (LEFT(@text, @index - 1)) 
     SET @text = RIGHT(@text, (LEN(@text) - @index)) 
     END 
    ELSE 
     SET @text = RIGHT(@text, (LEN(@text) - @index)) 
    END 
    RETURN 
END 



Select LEFT(value, Charindex(',', value) - 1) , 
RIGHT(value, Charindex(',', Reverse(value)) - 1) , 
* from [fn_Split_char] ('1,101|2,202|3,303', '|') 
+0

使用WHILE循環來分析字符串是非常低效的,不應該完成。還有其他的方法更好一些。 –

0

使用XML路徑和跨應用到基於單個行創建多個行在管隔板,然後使用子串WRT逗號來導出兩個期望列

Create table #temp(list nvarchar(max)) 
Insert into #temp values('1,101|2,202|3,303') 
SELECT 
    Substring(Tbl.Col.value('./text()[1]','varchar(50)'),1,1)as col1, 
    Substring(Tbl.Col.value('./text()[1]','varchar(50)'),charindex(',',Tbl.Col.value('./text()[1]','varchar(50)'),1)+1,len(Tbl.Col.value('./text()[1]','varchar(50)'))) 
FROM 
(Select cast('<a>'+ replace((SELECT list As [*] FOR XML PATH ('')), '|', '</a><a>') + '</a>' as xml)as t 
    from #temp) tl 
Cross apply 
tl.t.nodes('/a') AS Tbl(Col) 
0

嘗試使用此表值函數,嵌入此SP到主SP

ALTER FUNCTION [dbo].[delimiter] 
(
@PARAM_IDS AS VARCHAR(MAX) 
@PARAM_DELIMITER AS CHAR(1) 
) 
RETURNS 
@NEW_TABLE TABLE 
(
NUM INT NOT NULL IDENTITY, 
ID INT NOT NULL 
) 
AS 
BEGIN 
DECLARE @NEXTSTRING AS NVARCHAR(MAX); 
DECLARE @POS AS INT; 
DECLARE @STRING AS NVARCHAR(MAX); 
DECLARE @DELIMITER AS NVARCHAR(MAX); 
SET @STRING = @PARAM_IDS; 
SET @DELIMITER = @PARAM_DELIMITER; 
SET @STRING = @STRING + @DELIMITER; 
SET @POS = CHARINDEX(@DELIMITER,@STRING); 

WHILE (@POS <> 0) 
BEGIN 
    SET @NEXTSTRING = SUBSTRING(@STRING,1,@POS - 1); 
    INSERT @NEW_TABLE (ID) VALUES (@NEXTSTRING); 
    SET @STRING = SUBSTRING(@STRING,@POS+1,len(@STRING)); 
    SET @POS = CHARINDEX(@DELIMITER,@STRING); 
END 
    RETURN 
END 

然後例如使用

SET @DETAILS_COUNT = (SELECT COUNT(*) FROM delimiter(@PARAM_MS_UNIT_ID, @DELIMITER)); 
相關問題