如何将逗号分隔的值拆分为列

How to split a comma-separated value to columns

提问人:Gurru 提问时间:5/14/2012 最后编辑:AnonymousGurru 更新时间:3/3/2023 访问量:1295210

问:

我有一张这样的桌子

价值 字符串
1 克莱奥,史密斯

我想将逗号分隔的字符串分成两列

价值 名字
1 克里奥 史密斯

我只需要两个固定的额外列

sql-server sql-server-2008 csv

评论

0赞 Pondlife 5/14/2012
如何将单列值拆分为多列值的可能重复?
0赞 HABO 8/8/2020
来自 String_Split:“输出行可以按任何顺序排列。不保证顺序与输入字符串中子字符串的顺序匹配。它是在 SQL Server 2016 中添加的。
0赞 TylerH 9/24/2020
@HABO因此,对于如何在SQL Server 2008中执行此操作的问题,它是无用的:-)
0赞 Nick.Mc 8/10/2021
这里的大多数答案都将其拆分为行而不是列。所以大多数答案都是不正确的。

答:

168赞 Romil Kumar Jain 5/14/2012 #1

您的目的可以使用以下查询来解决 -

Select Value  , Substring(FullName, 1,Charindex(',', FullName)-1) as Name,
Substring(FullName, Charindex(',', FullName)+1, LEN(FullName)) as  Surname
from Table1

sql server 中没有现成的 Split 函数,因此我们需要创建用户定义的函数。

CREATE FUNCTION Split (
      @InputString                  VARCHAR(8000),
      @Delimiter                    VARCHAR(50)
)

RETURNS @Items TABLE (
      Item                          VARCHAR(8000)
)

AS
BEGIN
      IF @Delimiter = ' '
      BEGIN
            SET @Delimiter = ','
            SET @InputString = REPLACE(@InputString, ' ', @Delimiter)
      END

      IF (@Delimiter IS NULL OR @Delimiter = '')
            SET @Delimiter = ','

--INSERT INTO @Items VALUES (@Delimiter) -- Diagnostic
--INSERT INTO @Items VALUES (@InputString) -- Diagnostic

      DECLARE @Item           VARCHAR(8000)
      DECLARE @ItemList       VARCHAR(8000)
      DECLARE @DelimIndex     INT

      SET @ItemList = @InputString
      SET @DelimIndex = CHARINDEX(@Delimiter, @ItemList, 0)
      WHILE (@DelimIndex != 0)
      BEGIN
            SET @Item = SUBSTRING(@ItemList, 0, @DelimIndex)
            INSERT INTO @Items VALUES (@Item)

            -- Set @ItemList = @ItemList minus one less item
            SET @ItemList = SUBSTRING(@ItemList, @DelimIndex+1, LEN(@ItemList)-@DelimIndex)
            SET @DelimIndex = CHARINDEX(@Delimiter, @ItemList, 0)
      END -- End WHILE

      IF @Item IS NOT NULL -- At least one delimiter was encountered in @InputString
      BEGIN
            SET @Item = @ItemList
            INSERT INTO @Items VALUES (@Item)
      END

      -- No delimiters were encountered in @InputString, so just return @InputString
      ELSE INSERT INTO @Items VALUES (@InputString)

      RETURN

END -- End Function
GO

---- Set Permissions
--GRANT SELECT ON Split TO UserRole1
--GRANT SELECT ON Split TO UserRole2
--GO

评论

1赞 Ruskin 3/6/2018
在下面的答案中也请看 Jeff Moden @ughai的数字表解决方案 DelimitedSplit8K。
3赞 tvanharp 1/11/2019
SQL 2016 现在带有拆分函数
0赞 Alaa Alweish 12/30/2019
SQL 2016 及更高版本:SELECT * FROM STRING_SPLIT('John,Jeremy,Jack',',')
1赞 Brad Hein 9/15/2022
SUBSTRING 和 CHARINDEX 在 PySpark 中也有效,因此此答案解决了 #pyspark 上相同的列分隔挑战。
3赞 Michael Schnerring 5/14/2012 #2

您可能会发现 SQL 用户定义函数中用于分析分隔字符串的解决方案很有帮助(来自代码项目)。

这是此页面中的代码部分:

CREATE FUNCTION [fn_ParseText2Table]
  (@p_SourceText VARCHAR(MAX)
  ,@p_Delimeter VARCHAR(100)=',' --default to comma delimited.
  )
 RETURNS @retTable
  TABLE([Position] INT IDENTITY(1,1)
   ,[Int_Value] INT
   ,[Num_Value] NUMERIC(18,3)
   ,[Txt_Value] VARCHAR(MAX)
   ,[Date_value] DATETIME
   )
AS
/*
********************************************************************************
Purpose: Parse values from a delimited string
  & return the result as an indexed table
Copyright 1996, 1997, 2000, 2003 Clayton Groom (<A href="mailto:[email protected]">[email protected]</A>)
Posted to the public domain Aug, 2004
2003-06-17 Rewritten as SQL 2000 function.
 Reworked to allow for delimiters > 1 character in length
 and to convert Text values to numbers
2016-04-05 Added logic for date values based on "new" ISDATE() function, Updated to use XML approach, which is more efficient.
********************************************************************************
*/


BEGIN
 DECLARE @w_xml xml;
 SET @w_xml = N'<root><i>' + replace(@p_SourceText, @p_Delimeter,'</i><i>') + '</i></root>';


 INSERT INTO @retTable
     ([Int_Value]
    , [Num_Value]
    , [Txt_Value]
    , [Date_value]
     )
     SELECT CASE
       WHEN ISNUMERIC([i].value('.', 'VARCHAR(MAX)')) = 1
       THEN CAST(CAST([i].value('.', 'VARCHAR(MAX)') AS NUMERIC) AS INT)
      END AS [Int_Value]
    , CASE
       WHEN ISNUMERIC([i].value('.', 'VARCHAR(MAX)')) = 1
       THEN CAST([i].value('.', 'VARCHAR(MAX)') AS NUMERIC(18, 3))
      END AS [Num_Value]
    , [i].value('.', 'VARCHAR(MAX)') AS [txt_Value]
    , CASE
       WHEN ISDATE([i].value('.', 'VARCHAR(MAX)')) = 1
       THEN CAST([i].value('.', 'VARCHAR(MAX)') AS DATETIME)
      END AS [Num_Value]
     FROM @w_xml.nodes('//root/i') AS [Items]([i]);
 RETURN;
END;
GO

评论

11赞 Adam Lear 9/18/2013
你有没有机会在这里总结解决方案,以确保如果链接死了,答案不会过时?
69赞 bvr 2/27/2013 #3
;WITH Split_Names (Value,Name, xmlname)
AS
(
    SELECT Value,
    Name,
    CONVERT(XML,'<Names><name>'  
    + REPLACE(Name,',', '</name><name>') + '</name></Names>') AS xmlname
      FROM tblnames
)

 SELECT Value,      
 xmlname.value('/Names[1]/name[1]','varchar(100)') AS Name,    
 xmlname.value('/Names[1]/name[2]','varchar(100)') AS Surname
 FROM Split_Names

并检查下面的链接以供参考

http://jahaines.blogspot.in/2009/06/converting-delimited-string-of-values.html

评论

5赞 Kimchi Man 10/21/2014
这样更好..它简单而简短。
5赞 jotapdiez 6/4/2015
我真的很喜欢这种方式。当您有 2 个以上的值要拆分时,CHARINDEX 和 SUBSTRING 会一团糟(例如 1,2,3)。多谢
2赞 Michel de Ruiter 11/8/2016
好主意。不过,速度是加号混乱的三倍,至少对我来说是这样。:-(CHARINDEXSUBSTRING
5赞 Tony 3/28/2018
很好的解决方案,但是XML中的某些字符是非法的(例如“&”),因此我不得不将每个字段包装在CDATA标签中...CONVERT(XML,'<Names><name><![CDATA[' + REPLACE(Name,',', ']]></name><name><![CDATA[') + ']]></name></name>') AS xmlname
2赞 Ryan Buddicom 8/16/2018
@Tony需要将代码从 Tony 更新为 (缺少 </Names 上的最后 s>)CONVERT(XML,'<Names><name><![CDATA[' + REPLACE(address1,',', ']]></name><name><![CDATA[') + ']]></name></Names>') AS xmlname
52赞 aads 6/19/2013 #4

基于XML的答案简单明了

参考这个

DECLARE @S varchar(max),
        @Split char(1),
        @X xml

SELECT @S = 'ab,cd,ef,gh,ij',
       @Split = ','

SELECT @X = CONVERT(xml,' <root> <myvalue>' +
REPLACE(@S,@Split,'</myvalue> <myvalue>') + '</myvalue>   </root> ')

SELECT  T.c.value('.','varchar(20)'),              --retrieve ALL values at once
  T.c.value('(/root/myvalue)[1]','VARCHAR(20)')  , --retrieve index 1 only, which is the 'ab'
  T.c.value('(/root/myvalue)[2]','VARCHAR(20)')
 FROM @X.nodes('/root/myvalue') T(c)

评论

2赞 Vnge 2/8/2019
这真的很酷。类似数组的功能非常有用,我不知道。谢谢!
10赞 Himansz 3/26/2014 #5

我们可以创建一个函数,如下所示

CREATE Function [dbo].[fn_CSVToTable] 
(
    @CSVList Varchar(max)
)
RETURNS @Table TABLE (ColumnData VARCHAR(100))
AS
BEGIN
    IF RIGHT(@CSVList, 1) <> ','
    SELECT @CSVList = @CSVList + ','

    DECLARE @Pos    BIGINT,
            @OldPos BIGINT
    SELECT  @Pos    = 1,
            @OldPos = 1

    WHILE   @Pos < LEN(@CSVList)
        BEGIN
            SELECT  @Pos = CHARINDEX(',', @CSVList, @OldPos)
            INSERT INTO @Table
            SELECT  LTRIM(RTRIM(SUBSTRING(@CSVList, @OldPos, @Pos - @OldPos))) Col001

            SELECT  @OldPos = @Pos + 1
        END

    RETURN
END

然后,我们可以使用 SELECT 语句将 CSV 值分隔到各自的列中

50赞 Azar 6/20/2014 #6

我觉得这很酷

SELECT value,
    PARSENAME(REPLACE(String,',','.'),2) 'Name' ,
    PARSENAME(REPLACE(String,',','.'),1) 'Surname'
FROM table WITH (NOLOCK)

评论

3赞 Azar 7/8/2014
您只需要名字和姓氏
1赞 Luis Cazares 2/13/2019
您还需要注意,对于长度超过 128 个字符的项目,PARSENAME 将返回 NULL。
0赞 glass_kites 7/17/2019
好。也适用于我的数据集!
3赞 Aung Myo Linn 12/30/2014 #7

我的表:

Value  ColOne
--------------------
1      Cleo, Smith

如果列数不多,则以下操作应该有效

ALTER TABLE mytable ADD ColTwo nvarchar(256);
UPDATE mytable SET ColTwo = LEFT(ColOne, Charindex(',', ColOne) - 1);
--'Cleo' = LEFT('Cleo, Smith', Charindex(',', 'Cleo, Smith') - 1)
UPDATE mytable SET ColTwo = REPLACE(ColOne, ColTwo + ',', '');
--' Smith' = REPLACE('Cleo, Smith', 'Cleo' + ',')
UPDATE mytable SET ColOne = REPLACE(ColOne, ',' + ColTwo, ''), ColTwo = LTRIM(ColTwo);
--'Cleo' = REPLACE('Cleo, Smith', ',' + ' Smith', '') 

结果:

Value  ColOne ColTwo
--------------------
1      Cleo   Smith
7赞 Mohammad Karimi 3/18/2015 #8

使用 Parsename() 函数

with cte as(
    select 'Aria,Karimi' as FullName
    Union
    select 'Joe,Karimi' as FullName
    Union
    select 'Bab,Karimi' as FullName
)

SELECT PARSENAME(REPLACE(FullName,',','.'),2) as Name, 
       PARSENAME(REPLACE(FullName,',','.'),1) as Family
    FROM cte

结果

Name    Family
-----   ------
Aria    Karimi
Bab     Karimi
Joe     Karimi
17赞 anonymous 4/15/2015 #9
SELECT id,
       Substring(NAME, 0, Charindex(',', NAME))             AS firstname,
       Substring(NAME, Charindex(',', NAME), Len(NAME) + 1) AS lastname
FROM   spilt  

评论

6赞 Politank-Z 4/15/2015
如果您可以扩展您的答案,并使用代码格式化工具,那将很有用。
0赞 LarryBud 6/12/2020
关闭,这将在姓氏中包含逗号。在错误的位置得到了 +1。应为 Substring(NAME, Charindex(',', NAME)+1, Len(NAME)) AS lastname
0赞 Rahul Shukla 2/17/2022
上面的查询也给出了带有 , (逗号) 的姓氏,因为它以 开头。下面是正确的 SELECT id, Substring(NAME, 0, Charindex(',', NAME)) AS firstname, Substring(NAME, Charindex(',', NAME)+1, Len(NAME) ) AS lastname FROM spilt
0赞 Rahul Bhat 12/30/2022
如果出现多个逗号分隔值,会发生什么情况?
2赞 RoadRunner 4/28/2015 #10

我发现如上所述使用PARSENAME会导致任何带有句点的名称被清空。

因此,如果名称中有一个首字母或标题,后跟一个点,则返回 NULL。

我发现这对我有用:

SELECT 
REPLACE(SUBSTRING(FullName, 1,CHARINDEX(',', FullName)), ',','') as Name,
REPLACE(SUBSTRING(FullName, CHARINDEX(',', FullName), LEN(FullName)), ',', '') as Surname
FROM Table1
30赞 Lavisa 5/14/2015 #11

使用交叉应用

select ParsedData.* 
from MyTable mt
cross apply ( select str = mt.String + ',,' ) f1
cross apply ( select p1 = charindex( ',', str ) ) ap1
cross apply ( select p2 = charindex( ',', str, p1 + 1 ) ) ap2
cross apply ( select Nmame = substring( str, 1, p1-1 )                   
                 , Surname = substring( str, p1+1, p2-p1-1 )
          ) ParsedData

评论

5赞 tomato 10/8/2015
我无法理解为什么您需要在原始字符串的末尾添加 2 个逗号才能使其正常工作。为什么没有“+',,' ”就不起作用?
0赞 Waller 1/17/2017
@developer.ejay 是因为 Left/SubString 函数不能取 0 值吗?
0赞 Mike 11/26/2019
伟大!您可以轻松地为所需的每个额外列复制/粘贴 2 行 - 然后只需递增数字,例如:select ParsedData.* from MyTable mt cross apply ( select str = mt.字符串 + ',,' ) f1 交叉应用 ( select p1 = charindex( ',', str ) ) ap1 交叉应用 ( select p2 = charindex( ',', str, p1 + 1 ) ) ap2 交叉应用 ( select p3 = charindex( ',', str, p2 + 1 ) ) ap3 交叉应用 ( select FName = substring( str, 1, p1-1 ) , LName = substring( str, p1+1, p2-p1-1 ) , Age = substring( str, p2+1, p3-p2-1 ) ) 解析数据
24赞 ughai 6/11/2015 #12

有多种方法可以解决这个问题,并且已经提出了许多不同的方法。最简单的方法是使用 / 和其他字符串函数来实现所需的结果。LEFTSUBSTRING

示例数据

DECLARE @tbl1 TABLE (Value INT,String VARCHAR(MAX))

INSERT INTO @tbl1 VALUES(1,'Cleo, Smith');
INSERT INTO @tbl1 VALUES(2,'John, Mathew');

使用字符串函数,如 LEFT

SELECT
    Value,
    LEFT(String,CHARINDEX(',',String)-1) as Fname,
    LTRIM(RIGHT(String,LEN(String) - CHARINDEX(',',String) )) AS Lname
FROM @tbl1

如果 String 中有更多的 2 个项目,则此方法将失败。 在这种情况下,我们可以使用拆分器,然后使用或将字符串转换为 和 用于获取字符串项。 AADS和BVR在其解决方案中详细介绍了基于解决方案的解决方案。PIVOTXML.nodesXML

这个问题的答案使用拆分器,所有使用拆分器都是低效的。查看此性能比较。最好的分离器之一是 ,由 Jeff Moden 创建。你可以在这里阅读更多关于它的信息WHILEDelimitedSplit8K

PIVOT 的分路器

DECLARE @tbl1 TABLE (Value INT,String VARCHAR(MAX))

INSERT INTO @tbl1 VALUES(1,'Cleo, Smith');
INSERT INTO @tbl1 VALUES(2,'John, Mathew');


SELECT t3.Value,[1] as Fname,[2] as Lname
FROM @tbl1 as t1
CROSS APPLY [dbo].[DelimitedSplit8K](String,',') as t2
PIVOT(MAX(Item) FOR ItemNumber IN ([1],[2])) as t3

输出

Value   Fname   Lname
1   Cleo    Smith
2   John    Mathew

DelimitedSplit8K 的 Jeff Moden

CREATE FUNCTION [dbo].[DelimitedSplit8K]
/**********************************************************************************************************************
 Purpose:
 Split a given string at a given delimiter and return a list of the split elements (items).

 Notes:
 1.  Leading a trailing delimiters are treated as if an empty string element were present.
 2.  Consecutive delimiters are treated as if an empty string element were present between them.
 3.  Except when spaces are used as a delimiter, all spaces present in each element are preserved.

 Returns:
 iTVF containing the following:
 ItemNumber = Element position of Item as a BIGINT (not converted to INT to eliminate a CAST)
 Item       = Element value as a VARCHAR(8000)

 Statistics on this function may be found at the following URL:
 http://www.sqlservercentral.com/Forums/Topic1101315-203-4.aspx

 CROSS APPLY Usage Examples and Tests:
--=====================================================================================================================
-- TEST 1:
-- This tests for various possible conditions in a string using a comma as the delimiter.  The expected results are
-- laid out in the comments
--=====================================================================================================================
--===== Conditionally drop the test tables to make reruns easier for testing.
     -- (this is NOT a part of the solution)
     IF OBJECT_ID('tempdb..#JBMTest') IS NOT NULL DROP TABLE #JBMTest
;
--===== Create and populate a test table on the fly (this is NOT a part of the solution).
     -- In the following comments, "b" is a blank and "E" is an element in the left to right order.
     -- Double Quotes are used to encapsulate the output of "Item" so that you can see that all blanks
     -- are preserved no matter where they may appear.
 SELECT *
   INTO #JBMTest
   FROM (                                               --# & type of Return Row(s)
         SELECT  0, NULL                      UNION ALL --1 NULL
         SELECT  1, SPACE(0)                  UNION ALL --1 b (Empty String)
         SELECT  2, SPACE(1)                  UNION ALL --1 b (1 space)
         SELECT  3, SPACE(5)                  UNION ALL --1 b (5 spaces)
         SELECT  4, ','                       UNION ALL --2 b b (both are empty strings)
         SELECT  5, '55555'                   UNION ALL --1 E
         SELECT  6, ',55555'                  UNION ALL --2 b E
         SELECT  7, ',55555,'                 UNION ALL --3 b E b
         SELECT  8, '55555,'                  UNION ALL --2 b B
         SELECT  9, '55555,1'                 UNION ALL --2 E E
         SELECT 10, '1,55555'                 UNION ALL --2 E E
         SELECT 11, '55555,4444,333,22,1'     UNION ALL --5 E E E E E 
         SELECT 12, '55555,4444,,333,22,1'    UNION ALL --6 E E b E E E
         SELECT 13, ',55555,4444,,333,22,1,'  UNION ALL --8 b E E b E E E b
         SELECT 14, ',55555,4444,,,333,22,1,' UNION ALL --9 b E E b b E E E b
         SELECT 15, ' 4444,55555 '            UNION ALL --2 E (w/Leading Space) E (w/Trailing Space)
         SELECT 16, 'This,is,a,test.'                   --E E E E
        ) d (SomeID, SomeValue)
;
--===== Split the CSV column for the whole table using CROSS APPLY (this is the solution)
 SELECT test.SomeID, test.SomeValue, split.ItemNumber, Item = QUOTENAME(split.Item,'"')
   FROM #JBMTest test
  CROSS APPLY dbo.DelimitedSplit8K(test.SomeValue,',') split
;
--=====================================================================================================================
-- TEST 2:
-- This tests for various "alpha" splits and COLLATION using all ASCII characters from 0 to 255 as a delimiter against
-- a given string.  Note that not all of the delimiters will be visible and some will show up as tiny squares because
-- they are "control" characters.  More specifically, this test will show you what happens to various non-accented 
-- letters for your given collation depending on the delimiter you chose.
--=====================================================================================================================
WITH 
cteBuildAllCharacters (String,Delimiter) AS 
(
 SELECT TOP 256 
        'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
        CHAR(ROW_NUMBER() OVER (ORDER BY (SELECT NULL))-1)
   FROM master.sys.all_columns
)
 SELECT ASCII_Value = ASCII(c.Delimiter), c.Delimiter, split.ItemNumber, Item = QUOTENAME(split.Item,'"')
   FROM cteBuildAllCharacters c
  CROSS APPLY dbo.DelimitedSplit8K(c.String,c.Delimiter) split
  ORDER BY ASCII_Value, split.ItemNumber
;
-----------------------------------------------------------------------------------------------------------------------
 Other Notes:
 1. Optimized for VARCHAR(8000) or less.  No testing or error reporting for truncation at 8000 characters is done.
 2. Optimized for single character delimiter.  Multi-character delimiters should be resolvedexternally from this 
    function.
 3. Optimized for use with CROSS APPLY.
 4. Does not "trim" elements just in case leading or trailing blanks are intended.
 5. If you don't know how a Tally table can be used to replace loops, please see the following...
    http://www.sqlservercentral.com/articles/T-SQL/62867/
 6. Changing this function to use NVARCHAR(MAX) will cause it to run twice as slow.  It's just the nature of 
    VARCHAR(MAX) whether it fits in-row or not.
 7. Multi-machine testing for the method of using UNPIVOT instead of 10 SELECT/UNION ALLs shows that the UNPIVOT method
    is quite machine dependent and can slow things down quite a bit.
-----------------------------------------------------------------------------------------------------------------------
 Credits:
 This code is the product of many people's efforts including but not limited to the following:
 cteTally concept originally by Iztek Ben Gan and "decimalized" by Lynn Pettis (and others) for a bit of extra speed
 and finally redacted by Jeff Moden for a different slant on readability and compactness. Hat's off to Paul White for
 his simple explanations of CROSS APPLY and for his detailed testing efforts. Last but not least, thanks to
 Ron "BitBucket" McCullough and Wayne Sheffield for their extreme performance testing across multiple machines and
 versions of SQL Server.  The latest improvement brought an additional 15-20% improvement over Rev 05.  Special thanks
 to "Nadrek" and "peter-757102" (aka Peter de Heer) for bringing such improvements to light.  Nadrek's original
 improvement brought about a 10% performance gain and Peter followed that up with the content of Rev 07.  

 I also thank whoever wrote the first article I ever saw on "numbers tables" which is located at the following URL
 and to Adam Machanic for leading me to it many years ago.
 http://sqlserver2000.databases.aspfaq.com/why-should-i-consider-using-an-auxiliary-numbers-table.html
-----------------------------------------------------------------------------------------------------------------------
 Revision History:
 Rev 00 - 20 Jan 2010 - Concept for inline cteTally: Lynn Pettis and others.
                        Redaction/Implementation: Jeff Moden 
        - Base 10 redaction and reduction for CTE.  (Total rewrite)

 Rev 01 - 13 Mar 2010 - Jeff Moden
        - Removed one additional concatenation and one subtraction from the SUBSTRING in the SELECT List for that tiny
          bit of extra speed.

 Rev 02 - 14 Apr 2010 - Jeff Moden
        - No code changes.  Added CROSS APPLY usage example to the header, some additional credits, and extra 
          documentation.

 Rev 03 - 18 Apr 2010 - Jeff Moden
        - No code changes.  Added notes 7, 8, and 9 about certain "optimizations" that don't actually work for this
          type of function.

 Rev 04 - 29 Jun 2010 - Jeff Moden
        - Added WITH SCHEMABINDING thanks to a note by Paul White.  This prevents an unnecessary "Table Spool" when the
          function is used in an UPDATE statement even though the function makes no external references.

 Rev 05 - 02 Apr 2011 - Jeff Moden
        - Rewritten for extreme performance improvement especially for larger strings approaching the 8K boundary and
          for strings that have wider elements.  The redaction of this code involved removing ALL concatenation of 
          delimiters, optimization of the maximum "N" value by using TOP instead of including it in the WHERE clause,
          and the reduction of all previous calculations (thanks to the switch to a "zero based" cteTally) to just one 
          instance of one add and one instance of a subtract. The length calculation for the final element (not 
          followed by a delimiter) in the string to be split has been greatly simplified by using the ISNULL/NULLIF 
          combination to determine when the CHARINDEX returned a 0 which indicates there are no more delimiters to be
          had or to start with. Depending on the width of the elements, this code is between 4 and 8 times faster on a
          single CPU box than the original code especially near the 8K boundary.
        - Modified comments to include more sanity checks on the usage example, etc.
        - Removed "other" notes 8 and 9 as they were no longer applicable.

 Rev 06 - 12 Apr 2011 - Jeff Moden
        - Based on a suggestion by Ron "Bitbucket" McCullough, additional test rows were added to the sample code and
          the code was changed to encapsulate the output in pipes so that spaces and empty strings could be perceived 
          in the output.  The first "Notes" section was added.  Finally, an extra test was added to the comments above.

 Rev 07 - 06 May 2011 - Peter de Heer, a further 15-20% performance enhancement has been discovered and incorporated 
          into this code which also eliminated the need for a "zero" position in the cteTally table. 
**********************************************************************************************************************/
--===== Define I/O parameters
        (@pString VARCHAR(8000), @pDelimiter CHAR(1))
RETURNS TABLE WITH SCHEMABINDING AS
 RETURN
--===== "Inline" CTE Driven "Tally Table" produces values from 0 up to 10,000...
     -- enough to cover NVARCHAR(4000)
  WITH E1(N) AS (
                 SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL 
                 SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL 
                 SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
                ),                          --10E+1 or 10 rows
       E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows
       E4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E+4 or 10,000 rows max
 cteTally(N) AS (--==== This provides the "base" CTE and limits the number of rows right up front
                     -- for both a performance gain and prevention of accidental "overruns"
                 SELECT TOP (ISNULL(DATALENGTH(@pString),0)) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4
                ),
cteStart(N1) AS (--==== This returns N+1 (starting position of each "element" just once for each delimiter)
                 SELECT 1 UNION ALL
                 SELECT t.N+1 FROM cteTally t WHERE SUBSTRING(@pString,t.N,1) = @pDelimiter
                ),
cteLen(N1,L1) AS(--==== Return start and length (for use in substring)
                 SELECT s.N1,
                        ISNULL(NULLIF(CHARINDEX(@pDelimiter,@pString,s.N1),0)-s.N1,8000)
                   FROM cteStart s
                )
--===== Do the actual split. The ISNULL/NULLIF combo handles the length for the final element when no delimiter is found.
 SELECT ItemNumber = ROW_NUMBER() OVER(ORDER BY l.N1),
        Item       = SUBSTRING(@pString, l.N1, l.L1)
   FROM cteLen l
;

GO
5赞 Yossi 7/14/2015 #13

我遇到了一个类似的问题,但很复杂,因为这是我发现的关于该问题的第一个线程,所以我决定发布我的发现。我知道这是一个简单问题的复杂解决方案,但我希望我可以帮助其他寻找更复杂解决方案的人。我不得不拆分一个包含 5 个数字的字符串(列名:levelsFeed)并在单独的列中显示每个数字。 例如:8,1,2,2,2 应显示为:

1  2  3  4  5
-------------
8  1  2  2  2

解决方案 1:使用 XML 函数: 该解决方案是迄今为止最慢的解决方案

SELECT Distinct FeedbackID, 
, S.a.value('(/H/r)[1]', 'INT') AS level1
, S.a.value('(/H/r)[2]', 'INT') AS level2
, S.a.value('(/H/r)[3]', 'INT') AS level3
, S.a.value('(/H/r)[4]', 'INT') AS level4
, S.a.value('(/H/r)[5]', 'INT') AS level5
FROM (            
    SELECT *,CAST (N'<H><r>' + REPLACE(levelsFeed, ',', '</r><r>')  + '</r> </H>' AS XML) AS [vals]
    FROM Feedbacks 
)  as d
CROSS APPLY d.[vals].nodes('/H/r') S(a)

解决方案2:使用拆分功能和枢轴。(split 函数将字符串拆分为列名为 Data 的行)

SELECT FeedbackID, [1],[2],[3],[4],[5]
FROM (
    SELECT *, ROW_NUMBER() OVER (PARTITION BY feedbackID ORDER BY (SELECT  null)) as rn 
FROM (
    SELECT FeedbackID, levelsFeed
    FROM Feedbacks 
) as a
CROSS APPLY dbo.Split(levelsFeed, ',')
) as SourceTable
PIVOT
(
    MAX(data)
    FOR rn IN ([1],[2],[3],[4],[5])
)as pivotTable

解决方案 3:使用字符串操作函数 - 比解决方案 2 快得多

SELECT FeedbackID,
SUBSTRING(levelsFeed,0,CHARINDEX(',',levelsFeed)) AS level1,
PARSENAME(REPLACE(SUBSTRING(levelsFeed,CHARINDEX(',',levelsFeed)+1,LEN(levelsFeed)),',','.'),4) AS level2,
PARSENAME(REPLACE(SUBSTRING(levelsFeed,CHARINDEX(',',levelsFeed)+1,LEN(levelsFeed)),',','.'),3) AS level3,
PARSENAME(REPLACE(SUBSTRING(levelsFeed,CHARINDEX(',',levelsFeed)+1,LEN(levelsFeed)),',','.'),2) AS level4,
PARSENAME(REPLACE(SUBSTRING(levelsFeed,CHARINDEX(',',levelsFeed)+1,LEN(levelsFeed)),',','.'),1) AS level5
FROM Feedbacks

由于 levelsFeed 包含 5 个字符串值,因此我需要对第一个字符串使用子字符串函数。

我希望我的解决方案能帮助其他找到这个线程的人寻找更复杂的拆分到列方法

5赞 WoodChopper 9/30/2015 #14

:)使用 instring 函数

select Value, 
       substring(String,1,instr(String," ") -1) Fname,  
       substring(String,instr(String,",") +1) Sname 
from tablename;

使用了两个功能,
1. ==> 返回从位置到长度
2 的字符串。 ==> 返回模式的位置。
substring(string, position, length)instr(string,pattern)

如果我们没有在子字符串中提供长度参数,它会返回直到字符串末尾

评论

1赞 Peter B 12/10/2015
不确定您使用的是哪种 SQL 方言,但在 SQL Server 中,我们必须使用类似 后跟 .substring(@str, 1, charindex(@sep, @str) - 1)substring(@str, charindex(@sep, @str) + 1, len(@str))
0赞 Glyn 10/16/2015 #15
CREATE FUNCTION [dbo].[fnSplit](@sInputList VARCHAR(8000), @sDelimiter VARCHAR(8000) = ',')
RETURNS @List TABLE (item VARCHAR(8000))
BEGIN

    DECLARE @sItem VARCHAR(8000)
    WHILE CHARINDEX(@sDelimiter, @sInputList, 0) <> 0
    BEGIN

        SELECT @sItem = RTRIM(LTRIM(SUBSTRING(@sInputList, 1, CHARINDEX(@sDelimiter, @sInputList,0) - 1))),
               @sInputList = RTRIM(LTRIM(SUBSTRING(@sInputList, CHARINDEX(@sDelimiter, @sInputList, 0) + LEN(@sDelimiter),LEN(@sInputList))))

        -- Indexes to keep the position of searching
        IF LEN(@sItem) > 0

        INSERT INTO @List SELECT @sItem

    END

    IF LEN(@sInputList) > 0
    BEGIN

        INSERT INTO @List SELECT @sInputList -- Put the last item in

    END

    RETURN

END
14赞 JasonP 12/23/2015 #16

我认为PARSENAME是用于此示例的简洁函数,如本文所述: http://www.sqlshack.com/parsing-and-rotating-delimited-data-in-sql-server-2012/

PARSENAME 函数在逻辑上设计用于分析由四部分组成的对象名称。PARSENAME 的好处在于,它不仅限于分析 SQL Server 的四部分对象名称,它还将分析由点分隔的任何函数或字符串数据。

第一个参数是要解析的对象,第二个参数是要返回的对象片段的整数值。本文讨论的是解析和轮换分隔数据 - 公司电话号码,但它也可用于解析姓名/姓氏数据。

例:

USE COMPANY;
SELECT PARSENAME('Whatever.you.want.parsed',3) AS 'ReturnValue';

本文还介绍了如何使用名为“replaceChars”的公用表表达式 (CTE) 对分隔符替换的值运行 PARSENAME。CTE 可用于返回临时视图或结果集。

之后,UNPIVOT 函数已用于将某些列转换为行;SUBSTRING 和 CHARINDEX 函数已用于清理数据中的不一致,并且最终使用了 LAG 函数(SQL Server 2012 的新增功能),因为它允许引用以前的记录。

7赞 Rangani 3/14/2016 #17

试试这个:

declare @csv varchar(100) ='aaa,bb,csda,daass';
set @csv = @csv+',';

with cte as
(
    select SUBSTRING(@csv,1,charindex(',',@csv,1)-1) as val, SUBSTRING(@csv,charindex(',',@csv,1)+1,len(@csv)) as rem 
    UNION ALL
    select SUBSTRING(a.rem,1,charindex(',',a.rem,1)-1)as val, SUBSTRING(a.rem,charindex(',',a.rem,1)+1,len(A.rem)) 
    from cte a where LEN(a.rem)>=1
    ) select val from cte

评论

0赞 yu yang Jian 1/15/2019
像魅力一样工作!
2赞 Mehdi najafian 3/16/2016 #18

这太简单了,你可以通过下面的查询来获取它:

DECLARE @str NVARCHAR(MAX)='ControlID_05436b78-04ba-9667-fa01-9ff8c1b7c235,3'
SELECT LEFT(@str, CHARINDEX(',',@str)-1),RIGHT(@str,LEN(@str)-(CHARINDEX(',',@str)))
-2赞 amin.sanjar2 6/12/2016 #19
ALTER FUNCTION [dbo].[StringListTo] (@StringList Nvarchar(max),@Separators char(1),@start int, @index int )
RETURNS nvarchar(max)
AS
BEGIN
declare @out Nvarchar(max)
declare @i int
declare @start_old int
set @start=@start+1
set @i=1
while(@i<=@index)
begin
    set @start_old=@start
    set @start=CHARINDEX('.',@StringList,@start+1)
    if (@start>0)
    begin
        set @out=Substring(@StringList,@start_old+1,@start-@start_old-1)
    end
else
begin
    set @out=Substring(@StringList,@start_old+1,len(@StringList)-1)
end
set @i=@i+1
end
RETURN @out
END;

评论

3赞 Tim Malone 6/12/2016
嗨,阿明,欢迎来到 SO。不鼓励只使用代码答案,因为它们不会教任何人为什么这段代码可以解决问题。你能编辑你的帖子来解释这段代码的作用吗?
1赞 M H 9/23/2016
需要有关正在发生的事情以及为什么的详细信息,这只是复制和粘贴吗?
3赞 Narendra gudapati 8/10/2016 #20
DECLARE @INPUT VARCHAR (MAX)='N,A,R,E,N,D,R,A'
DECLARE @ELIMINATE_CHAR CHAR (1)=','
DECLARE @L_START INT=1
DECLARE @L_END INT=(SELECT LEN (@INPUT))
DECLARE @OUTPUT CHAR (1)

WHILE @L_START <=@L_END
BEGIN
    SET @OUTPUT=(SUBSTRING (@INPUT,@L_START,1))
    IF @OUTPUT!=@ELIMINATE_CHAR
    BEGIN
        PRINT @OUTPUT
    END
    SET @L_START=@L_START+1
END

评论

0赞 Wessam El Mahdy 3/7/2017
我使用了你的代码,它很简单,但是ELIMINATE_CHAT存在拼写错误,它应该ELIMINATE_CHAR并且应该L_START脚本末尾的 START。谢谢。
22赞 Kannan Kandasamy 9/8/2016 #21

在 SQL Server 2016 中,我们可以使用string_split来实现此目的:

create table commasep (
 id int identity(1,1)
 ,string nvarchar(100) )

insert into commasep (string) values ('John, Adam'), ('test1,test2,test3')

select id, [value] as String from commasep 
 cross apply string_split(string,',')

评论

0赞 Bruno 12/15/2016
我正在使用 SQL Server 2016,但它出现错误Invalid object name 'string_split'
3赞 Kannan Kandasamy 12/15/2016
您能检查数据库的兼容级别吗?它必须是 130,即 SQL Server 2016。可以使用此查询 select * from sys.databases
0赞 Bruno 12/15/2016
是的,我看到 120,所以它必须只是 2016 年的客户端(Microsoft SQL Server Management Studio),而不是数据库服务器本身,因为如果我转到“帮助”->“关于”,我会看到 SQL Server 2016 Management Studio v13.0.15000.23。谢谢
1赞 Joerg Krause 9/18/2019
即使实际安装的版本更高,数据库开发人员也可能将级别设置为任何较低的值以保持数据库兼容。只要数据库支持以下功能,就可以使用此选项将级别设置为所需的高级别:DECLARE @cl TINYINT; SELECT @cl = compatibility_level FROM [sys].[databases] WHERE name = 'mydb'; IF @cl < 130 BEGIN ALTER DATABASE myDb SET COMPATIBILITY_LEVEL = 130 END;
3赞 Guy Manova 6/9/2020
除非将其从行转回列,否则这是无用的。
1赞 Frank 12/9/2016 #22
select distinct modelFileId,F4.*
from contract
cross apply (select XmlList=convert(xml, '<x>'+replace(modelFileId,';','</x><x>')+'</x>').query('.')) F2
cross apply (select mfid1=XmlNode.value('/x[1]','varchar(512)')
,mfid2=XmlNode.value('/x[2]','varchar(512)')
,mfid3=XmlNode.value('/x[3]','varchar(512)')
,mfid4=XmlNode.value('/x[4]','varchar(512)') from XmlList.nodes('x') F3(XmlNode)) F4
where modelFileId like '%;%'
order by modelFileId
15赞 user7347410 12/28/2016 #23

试试这个(将 ' ' 的实例更改为 ',' 或您要使用的任何分隔符)

CREATE FUNCTION dbo.Wordparser
(
  @multiwordstring VARCHAR(255),
  @wordnumber      NUMERIC
)
returns VARCHAR(255)
AS
  BEGIN
      DECLARE @remainingstring VARCHAR(255)
      SET @remainingstring=@multiwordstring

      DECLARE @numberofwords NUMERIC
      SET @numberofwords=(LEN(@remainingstring) - LEN(REPLACE(@remainingstring, ' ', '')) + 1)

      DECLARE @word VARCHAR(50)
      DECLARE @parsedwords TABLE
      (
         line NUMERIC IDENTITY(1, 1),
         word VARCHAR(255)
      )

      WHILE @numberofwords > 1
        BEGIN
            SET @word=LEFT(@remainingstring, CHARINDEX(' ', @remainingstring) - 1)

            INSERT INTO @parsedwords(word)
            SELECT @word

            SET @remainingstring= REPLACE(@remainingstring, Concat(@word, ' '), '')
            SET @numberofwords=(LEN(@remainingstring) - LEN(REPLACE(@remainingstring, ' ', '')) + 1)

            IF @numberofwords = 1
              BREAK

            ELSE
              CONTINUE
        END

      IF @numberofwords = 1
        SELECT @word = @remainingstring
      INSERT INTO @parsedwords(word)
      SELECT @word

      RETURN
        (SELECT word
         FROM   @parsedwords
         WHERE  line = @wordnumber)

  END

用法示例:

SELECT dbo.Wordparser(COLUMN, 1),
       dbo.Wordparser(COLUMN, 2),
       dbo.Wordparser(COLUMN, 3)
FROM   TABLE

评论

0赞 Pete Alvin 4/16/2019
如果同一行中的相同值,我失败。
4赞 Krishna 1/7/2017 #24

这对我有用

CREATE FUNCTION [dbo].[SplitString](
    @delimited NVARCHAR(MAX),
    @delimiter NVARCHAR(100)
) RETURNS @t TABLE ( val NVARCHAR(MAX))
AS
BEGIN
    DECLARE @xml XML
    SET @xml = N'<t>' + REPLACE(@delimited,@delimiter,'</t><t>') + '</t>'
    INSERT INTO @t(val)
    SELECT  r.value('.','varchar(MAX)') as item
    FROM  @xml.nodes('/t') as records(r)
    RETURN
END

评论

0赞 Zach Smith 8/17/2017
您知道如何处理 XML 特殊字符吗?
0赞 vicky 1/9/2017 #25

您可以使用拆分功能。

SELECT 
(select top 1 item from dbo.Split(FullName,',') where id=1 ) as Name,
(select top 1 item from dbo.Split(FullName,',') where id=2 ) as Surname,
FROM MyTbl

评论

0赞 ashveli 10/17/2017
这不是内置函数。我们需要在该数据库中创建一个函数 Split。
1赞 user7678586 3/8/2017 #26
Select distinct PROJ_UID,PROJ_NAME,RES_UID from E2E_ProjectWiseTimesheetActuals
where   CHARINDEX(','+cast(PROJ_UID as varchar(8000))+',', @params) > 0 and  CHARINDEX(','+cast(RES_UID as varchar(8000))+',', @res) > 0

评论

4赞 Drag and Drop 3/8/2017
虽然此代码可以回答问题,但提供有关此代码为何和/或如何回答问题的其他上下文可以提高其长期价值。
9赞 bwanamaina 7/9/2017 #27

您可以使用表值函数,该函数仅在兼容级别 130 下可用。如果数据库兼容级别低于 130,则 SQL Server 将无法查找并执行该函数。您可以使用以下命令更改数据库的兼容级别:STRING_SPLITSTRING_SPLIT

ALTER DATABASE DatabaseName SET COMPATIBILITY_LEVEL = 130

语法

SELECT * FROM STRING_SPLIT ( string, separator )

请参阅此处的文档

评论

0赞 Lokesh 12/5/2018
好。但它不适用于低于 2016 年的 SQL Server
0赞 bwanamaina 12/8/2018
没错,在我的回答中,我表示它仅在兼容级别 130 及更高版本中可用。
8赞 geominded 4/30/2020
但是STRING_SPLIT拆分为多行,而不是每次拆分都拆分为多列。OP 询问的是拆分为多列,对吧?
9赞 Muhammad Awais 8/28/2017 #28

我认为以下功能将为您服务:

您必须先在 SQL 中创建一个函数。喜欢这个

CREATE FUNCTION [dbo].[fn_split](
@str VARCHAR(MAX),
@delimiter CHAR(1)
)
RETURNS @returnTable TABLE (idx INT PRIMARY KEY IDENTITY, item VARCHAR(8000))
AS
BEGIN
DECLARE @pos INT
SELECT @str = @str + @delimiter
WHILE LEN(@str) > 0 
    BEGIN
        SELECT @pos = CHARINDEX(@delimiter,@str)
        IF @pos = 1
            INSERT @returnTable (item)
                VALUES (NULL)
        ELSE
            INSERT @returnTable (item)
                VALUES (SUBSTRING(@str, 1, @pos-1))
        SELECT @str = SUBSTRING(@str, @pos+1, LEN(@str)-@pos)       
    END
RETURN
END

你可以调用这个函数,如下所示:

select * from fn_split('1,24,5',',')

实现:

Declare @test TABLE (
ID VARCHAR(200),
Data VARCHAR(200)
)

insert into @test 
(ID, Data)
Values
('1','Cleo,Smith')


insert into @test 
(ID, Data)
Values
('2','Paul,Grim')

select ID,
(select item from fn_split(Data,',') where idx in (1)) as Name ,
(select item from fn_split(Data,',') where idx in (2)) as Surname
 from @test

结果将是这样的:

enter image description here

评论

2赞 Sean Lange 10/5/2018
使用循环来拆分字符串的效率非常低。以下是该拆分函数的几个更好的选项。sqlperformance.com/2012/07/t-sql-queries/split-strings
7赞 Mariano Sedano 12/19/2017 #29

此功能最快:

CREATE FUNCTION dbo.F_ExtractSubString
(
  @String VARCHAR(MAX),
  @NroSubString INT,
  @Separator VARCHAR(5)
)
RETURNS VARCHAR(MAX) AS
BEGIN
    DECLARE @St INT = 0, @End INT = 0, @Ret VARCHAR(MAX)
    SET @String = @String + @Separator
    WHILE CHARINDEX(@Separator, @String, @End + 1) > 0 AND @NroSubString > 0
    BEGIN
        SET @St = @End + 1
        SET @End = CHARINDEX(@Separator, @String, @End + 1)
        SET @NroSubString = @NroSubString - 1
    END
    IF @NroSubString > 0
        SET @Ret = ''
    ELSE
        SET @Ret = SUBSTRING(@String, @St, @End - @St)
    RETURN @Ret
END
GO

用法示例:

SELECT dbo.F_ExtractSubString(COLUMN, 1, ', '),
       dbo.F_ExtractSubString(COLUMN, 2, ', '),
       dbo.F_ExtractSubString(COLUMN, 3, ', ')
FROM   TABLE

评论

3赞 Toby Speight 12/19/2017
感谢您提供此代码片段,它可能会提供一些有限的即时帮助。一个适当的解释将大大提高它的长期价值,说明为什么这是解决问题的一个很好的解决方案,并使它对未来有其他类似问题的读者更有用。请编辑您的答案以添加一些解释,包括您所做的假设。
-1赞 URMIL PREMAL SHAH 5/11/2018 #30

请尝试以下操作:

USE TRIAL
GO
CREATE TABLE DETAILS
(
  ID INT,
  NAME VARCHAR(50),
  ADDRESS VARCHAR(50)
)

INSERT INTO DETAILS
VALUES (100, 'POPE-JOHN-PAUL','VATICAN CIT|ROME|ITALY')
,(240, 'SIR-PAUL-McARTNEY','NEWYORK CITY|NEWYORK|USA')
,(460,'BARRACK-HUSSEIN-OBAMA','WHITE HOUSE|WASHINGTON|USA')
,(700, 'PRESIDENT-VLADAMIR-PUTIN','RED SQUARE|MOSCOW|RUSSIA')
,(950, 'NARENDRA-DAMODARDAS-MODI','10 JANPATH|NEW DELHI|INDIA')

查询:

select [ID]
,[NAME]
,[ADDRESS]
,REPLACE(LEFT(NAME, CHARINDEX('-', NAME)),'-',' ') as First_Name
,CASE 
WHEN CHARINDEX('-',REVERSE(NAME))+ CHARINDEX('-',NAME) < LEN(NAME)
THEN  SUBSTRING(NAME, CHARINDEX('-', (NAME)) + 1, LEN(NAME) - CHARINDEX('-', REVERSE(NAME)) - CHARINDEX('-', NAME))
ELSE 'NULL'
END AS Middle_Name
,REPLACE(REVERSE( SUBSTRING( REVERSE(NAME), 1, CHARINDEX('-',REVERSE(NAME)))), '-','') AS Last_Name 
,REPLACE(LEFT(ADDRESS, CHARINDEX('|', ADDRESS)),'|',' ') AS Locality
,CASE 
WHEN CHARINDEX('|',REVERSE(ADDRESS))+ CHARINDEX('|',ADDRESS) < LEN(ADDRESS) 
THEN SUBSTRING(ADDRESS, CHARINDEX('|', (ADDRESS))+1, LEN(ADDRESS)-CHARINDEX('|', REVERSE(ADDRESS))-CHARINDEX('|',ADDRESS))
ELSE 'Null' 
END AS STATE
,REPLACE(REVERSE(SUBSTRING(REVERSE(ADDRESS),1 ,CHARINDEX('|',REVERSE(ADDRESS)))),'|','') AS Country
FROM DETAILS

SELECT CHARINDEX('-', REVERSE(NAME)) AS LAST,CHARINDEX('-',NAME)AS FIRST, LEN(NAME) AS LENGTH
FROM DETAILS

SELECT SUBSTRING(NAME, CHARINDEX('-', (NAME))+1, LEN(NAME) -CHARINDEX('-', REVERSE(NAME)) - CHARINDEX('-', NAME))
FROM DETAILS

如果您对理解代码有任何疑问,请告诉我

评论

5赞 Ricky Smith 5/12/2018
请添加一些上下文来解释代码部分。
3赞 vignesh 5/22/2018 #31
ALTER function get_occurance_index(@delimiter varchar(1),@occurence int,@String varchar(100))
returns int
AS Begin
--Declare @delimiter varchar(1)=',',@occurence int=2,@String varchar(100)='a,b,c'
Declare @result int
 ;with T as (
    select 1 Rno,0 as row, charindex(@delimiter, @String) pos,@String st
    union all
    select Rno+1,pos + 1, charindex(@delimiter, @String, pos + 1), @String
    from T
    where pos > 0
)
select  @result=pos 
from T 
where pos > 0   and rno = @occurence 
return isnull(@result,0)
ENd


declare @data as table (data varchar(100))
insert into @data values('1,2,3') 
insert into @data values('aaa,bbbbb,cccc') 
select top  3 Substring (data,0,dbo.get_occurance_index( ',',1,data)) ,--First Record always starts with 0
Substring (data,dbo.get_occurance_index( ',',1,data)+1,dbo.get_occurance_index( ',',2,data)-dbo.get_occurance_index( ',',1,data)-1) ,
Substring (data,dbo.get_occurance_index( ',',2,data)+1,len(data)) , -- Last record cant be more than len of actual data
data 
From @data 
19赞 Blixter 2/28/2019 #32
CREATE FUNCTION [dbo].[fn_split_string_to_column] (
    @string NVARCHAR(MAX),
    @delimiter CHAR(1)
    )
RETURNS @out_put TABLE (
    [column_id] INT IDENTITY(1, 1) NOT NULL,
    [value] NVARCHAR(MAX)
    )
AS
BEGIN
    DECLARE @value NVARCHAR(MAX),
        @pos INT = 0,
        @len INT = 0

    SET @string = CASE 
            WHEN RIGHT(@string, 1) != @delimiter
                THEN @string + @delimiter
            ELSE @string
            END

    WHILE CHARINDEX(@delimiter, @string, @pos + 1) > 0
    BEGIN
        SET @len = CHARINDEX(@delimiter, @string, @pos + 1) - @pos
        SET @value = SUBSTRING(@string, @pos, @len)

        INSERT INTO @out_put ([value])
        SELECT LTRIM(RTRIM(@value)) AS [column]

        SET @pos = CHARINDEX(@delimiter, @string, @pos + @len) + 1
    END

    RETURN
END

评论

13赞 Shnugo 11/21/2019
这不应该是公认的答案......多语句 TVF(非常糟糕!)和一个循环(甚至更糟)一起表现会很糟糕。此外,这只是一个代码答案,甚至不能解决问题 周围有更好的方法!对于 SQL-Server 2016+,请查找(它不携带片段的位置,这是一个巨大的失败!)或非常快的 -hack。对于旧版本,请查找著名的 XML-hack(此处为 json 和 xml 详细信息)。或者寻找基于递归 CTE 的 iTVF 之一。WHILESTRING_SPLIT()JSON
0赞 Alaa Alweish 12/30/2019
SQL 2016 及更高版本:SELECT * FROM STRING_SPLIT('John,Jeremy,Jack',',')
0赞 Khatri 3/17/2020
同意给定的解决方案。但是,如果你是 SQL Server 2016,则可以使用string_split函数。您还可以在此处找到此内置函数的用法 tecloger.com/string-split-function-in-sql-server
7赞 geominded 4/30/2020
每个人都建议STRING_SPLIT,这个函数如何将字符串拆分为(而不是预期的行)?
3赞 Pete Alvin 4/16/2019 #33

我在上面重写了一个答案,并使其变得更好:

CREATE FUNCTION [dbo].[CSVParser]
(
  @s        VARCHAR(255),
  @idx      NUMERIC
)
RETURNS VARCHAR(12)
BEGIN
    DECLARE @comma int
    SET @comma = CHARINDEX(',', @s)
    WHILE 1=1
    BEGIN
        IF @comma=0
            IF @idx=1
                RETURN @s
            ELSE
                RETURN ''

        IF @idx=1
        BEGIN
            DECLARE @word VARCHAR(12)
            SET @word=LEFT(@s, @comma - 1)
            RETURN @word
        END

        SET @s = RIGHT(@s,LEN(@s)-@comma)
        SET @comma = CHARINDEX(',', @s)
        SET @idx = @idx - 1
    END
    RETURN 'not used'
END

用法示例:

SELECT dbo.CSVParser(COLUMN, 1),
       dbo.CSVParser(COLUMN, 2),
       dbo.CSVParser(COLUMN, 3)
FROM   TABLE
3赞 parfilko 11/23/2019 #34

问题很简单,但问题很热:)

因此,我为 string_split() 创建了一些包装器,这些包装器以更通用的方式进行枢轴。它是返回值(nn、value1、value2、...、value50)的表函数 - 对于大多数 CSV 行来说已经足够了。如果有更多值,它们将换行到下一行 - nn 表示行号。设置第三个参数 @columnCnt = [yourNumber] 以在特定位置换行:

alter FUNCTION fn_Split50
(   
    @str varchar(max),
    @delim char(1), 
    @columnCnt int = 50
)
RETURNS TABLE 
AS
RETURN 
(
    SELECT * 
    FROM (SELECT 
            nn = (nn - 1) / @columnCnt + 1, 
            nnn = 'value' + cast(((nn - 1) % @columnCnt) + 1 as varchar(10)), 
            value
        FROM (SELECT 
            nn = ROW_NUMBER() over (order by (select null)),
            value
            FROM string_split(@str, @delim) aa
            ) aa
        where nn > 0
    ) bb
    PIVOT  
    (  
    max(value) 
    FOR nnn IN (    
        value1, value2, value3, value4, value5, value6, value7, value8, value9, value10,
        value11, value12, value13, value14, value15, value16, value17, value18, value19, value20,
        value21, value22, value23, value24, value25, value26, value27, value28, value29, value30,
        value31, value32, value33, value34, value35, value36, value37, value38, value39, value40,
        value41, value42, value43, value44, value45, value46, value47, value48, value49, value50        
     )  
    ) AS PivotTable 
)

使用示例:

select * from dbo.fn_split50('zz1,aa2,ss3,dd4,ff5', ',', DEFAULT)

result 1

select * from dbo.fn_split50('zz1,aa2,ss3,dd4,ff5,gg6,hh7,jj8,ww9,qq10', ',', 3)

enter image description here

select * from dbo.fn_split50('zz1,11,aa2,22,ss3,33,dd4,44,ff5,55,gg6,66,hh7,77,jj8,88,ww9,99,qq10,1010', ',',2)

enter image description here

希望,它会有所帮助:)

评论

0赞 Srikanth Ayithy 11/9/2022
我有超过 4 个分隔符。我正在尝试用带有分隔符“_”的字符串转换一列,我想分成 10 个(尽可能多的分隔符)不同的列,列名是 col1、col2、col3 等。 '''' 创建表 dummy_table (MAIN_COLUMN varchar(max));插入dummy_table值 ('COL1_COL2_COL3_COL4_COL5_COL6_COL7_COL8_COL9_COL10'), ('COL1_COL2_COL3_COL4_COL5_COL6_COL7_COL8_COL9_COL10');预期结果:'''' |MAIN_COLUMN|COL_1 |COL_2 |..... |COL1_COL2_COL3_COL4_COL5_COL6_COL7_COL8_COL9_COL10|COL1 系列 |COL2 系列 |....
0赞 Masoud Safari 11/27/2019 #35

可以使用 SQL Server 函数:STRING_SPLIT

STRING_SPLIT ( string , separator )  

评论

0赞 Weles 2/17/2020
SQL Server 2016 及更高版本。请参阅:learn.microsoft.com/pt-br/sql/t-sql/functions/...
4赞 Rowland Shaw 3/3/2020
绝对值得注意的是,这是 SQL 2016 及更高版本,最初的问题是关于 2008 年的;但对于像我这样登陆这里的人来说仍然有用,使用更高版本:)
-1赞 ammad khan 3/27/2020 #36

试试这个

CREATE FUNCTION [dbo].[Split]  
(  
 @ListOfValues varchar(max),   
 @ValueSeparator varchar(10)  
)  
RETURNS @ListOfValuesInRows TABLE  
(  
 Value varchar(max)  
)  
AS  
BEGIN  

 IF Len(@ListOfValues) = 0  
  RETURN   

 if @ValueSeparator <> ' '  
 Begin  
  WHILE CHARINDEX(@ValueSeparator, @ListOfValues) > 0  
  BEGIN  

   INSERT INTO @ListOfValuesInRows   
   SELECT LTRIM(RTRIM(SUBSTRING(@ListOfValues, 1, CHARINDEX(@ValueSeparator, @ListOfValues)-1)))  

   SET @ListOfValues = SubString(@ListOfValues, CharIndex(@ValueSeparator, @ListOfValues)+Len(@ValueSeparator), Len(@ListOfValues))  

  END  

  INSERT INTO @ListOfValuesInRows  
  SELECT LTRIM(RTRIM(@ListOfValues))  
 End  
 Else  
 BEGIN  
  DECLARE @xml XML;  
  SET @xml = N'<t>' + REPLACE(@ListOfValues, @ValueSeparator, '</t><t>') + '</t>';  
  INSERT INTO @ListOfValuesInRows (Value)  
  SELECT LTRIM(RTRIM(r.value( '.', 'varchar(MAX)' ))) AS item  
  FROM @xml.nodes( '/t' ) AS records( r )  

 END  

RETURN  

END  
0赞 Zhorov 4/7/2021 #37

这是一个老问题,但如果可以升级到 SQL Server 2017+,基于 JSON 的方法也是一种选择。这个想法是进行适当的转换:

  • 将列中存储的文本转换为有效的 JSON 数组 ( into ),并使用 解析此数组。StringCleo, Smith["Cleo"," Smith"]JSON_VALUE()

  • 将列中存储的文本转换为有效的嵌套 JSON 数组 ( into ),并使用显式架构(列定义)解析此数组。StringCleo, Smith[["Cleo"," Smith"]]OPENJSON()

桌子:

SELECT [Value], [String]
INTO Data
FROM (VALUES
   (1, 'Cleo, Smith'),
   (2, 'John, Smith'),
   (3, 'Marian')
) v ([Value], [String])

语句使用:JSON_VALUE()

SELECT 
   [Value], 
   TRIM(JSON_VALUE(CONCAT('["', REPLACE(STRING_ESCAPE([String], 'json'), ',', '","'), '"]'), 'lax $[0]')) AS Name,
   TRIM(JSON_VALUE(CONCAT('["', REPLACE(STRING_ESCAPE([String], 'json'), ',', '","'), '"]'), 'lax $[1]')) AS Surname
FROM Data

语句使用:OPENJSON()

SELECT d.[Value], TRIM(j.[Name]) AS [Name], TRIM(j.[Surname]) AS [Surname]
FROM Data d
OUTER APPLY OPENJSON(CONCAT('[["', REPLACE(STRING_ESCAPE(d.[String], 'json'), ',', '","'), '"]]')) WITH (
   Name varchar(100) 'lax $[0]',
   Surname varchar(100) 'lax $[1]'
) j

结果:

Value  Name   Surname
---------------------
1      Cleo   Smith
2      John   Smith
3      Marian   

另外需要注意的是,使用此技术,您可以通过添加适当的 JSON 来轻松解析包含两列以上列的文本。path

1赞 Paulo Moreira 7/28/2022 #38

我做了:

drop table if exists #test;
create table #test(valor varchar(200));
insert into #test values ('Cleo, Smith'), ('Jhon');
select
    *
    ,REVERSE(PARSENAME(REPLACE(REVERSE(valor), ',', '.'), 1)) as name
     ,REVERSE(PARSENAME(REPLACE(REVERSE(valor), ',', '.'), 2)) as Surname
    ,REVERSE(PARSENAME(REPLACE(REVERSE(valor), ',', '.'), 3)) as other
from #test;

/*
+-----------+----+-------+-----+
|valor      |name|Surname|other|
+-----------+----+-------+-----+
|Cleo, Smith|Cleo| Smith |NULL |
|Jhon       |Jhon|NULL   |NULL |
+-----------+----+-------+-----+
*/

评论

0赞 cristiandatum 8/22/2022
PARSENAME 函数之所以有效,是因为该函数能够从“点”分隔字符串中的指定位置返回值。它确实有一个限制,即它不能在一个字符串中处理超过四个分隔的值。