SQL Server:拆分操作

时间:2010-03-24 11:51:27

标签: sql-server-2008 split

如何在SQL Server中拆分字符串。

示例:

输入字符串:stack over flow

结果:

stack
over
flow

5 个答案:

答案 0 :(得分:10)

如果您不能使用表值参数,请参阅:"Arrays and Lists in SQL Server 2008 Using Table-Valued Parameters" by Erland Sommarskog ,那么有很多方法可以在SQL Server中拆分字符串。本文涵盖几乎所有方法的PRO和CON:

"Arrays and Lists in SQL Server 2005 and Beyond, When Table Value Parameters Do Not Cut it" by Erland Sommarskog

您需要创建拆分功能。这就是如何使用拆分功能:

SELECT
    *
    FROM YourTable                               y
    INNER JOIN dbo.yourSplitFunction(@Parameter) s ON y.ID=s.Value

I prefer the number table approach to split a string in TSQL但是有很多方法可以在SQL Server中拆分字符串,请参阅上一个链接,该链接解释了每个链接的PRO和CON。

要使Numbers Table方法起作用,您需要进行一次性表设置,这将创建一个包含1到10,000行的表Numbers

SELECT TOP 10000 IDENTITY(int,1,1) AS Number
    INTO Numbers
    FROM sys.objects s1
    CROSS JOIN sys.objects s2
ALTER TABLE Numbers ADD CONSTRAINT PK_Numbers PRIMARY KEY CLUSTERED (Number)

设置Numbers表后,创建此拆分功能:

CREATE FUNCTION [dbo].[FN_ListToTable]
(
     @SplitOn  char(1)      --REQUIRED, the character to split the @List string on
    ,@List     varchar(8000)--REQUIRED, the list to split apart
)
RETURNS TABLE
AS
RETURN 
(

    ----------------
    --SINGLE QUERY-- --this will not return empty rows
    ----------------
    SELECT
        ListValue
        FROM (SELECT
                  LTRIM(RTRIM(SUBSTRING(List2, number+1, CHARINDEX(@SplitOn, List2, number+1)-number - 1))) AS ListValue
                  FROM (
                           SELECT @SplitOn + @List + @SplitOn AS List2
                       ) AS dt
                      INNER JOIN Numbers n ON n.Number < LEN(dt.List2)
                  WHERE SUBSTRING(List2, number, 1) = @SplitOn
             ) dt2
        WHERE ListValue IS NOT NULL AND ListValue!=''

);
GO 

您现在可以轻松地将CSV字符串拆分为表格并加入其中:

select * from dbo.FN_ListToTable(' ','stack over flow')

输出:

ListValue
-------------------
stack
over
flow

(3 row(s) affected)

答案 1 :(得分:2)

针对此类问题的常见基于集合的解决方案是使用数字表。

以下解决方案使用简单的递归CTE动态生成数字表 - 如果需要使用更长的字符串,则应将其替换为静态数字表。

DECLARE @vch_string varchar(max)
DECLARE @chr_delim char(1)
SET @chr_delim = ' '
SET @vch_string = 'stack over flow'

;WITH nums_cte
AS
(
  SELECT 1 AS n
  UNION ALL
  SELECT n+1 FROM nums_cte
  WHERE n < len(@vch_string)
)
SELECT n - LEN(REPLACE(LEFT(s,n),@chr_delim,'')) + 1 AS pos
       ,SUBSTRING(s,n,CHARINDEX(@chr_delim, s + @chr_delim,n) -n) as ELEMENT
FROM (SELECT @vch_string as s) AS D
JOIN nums_cte
ON n <= LEN(s)
AND SUBSTRING(@chr_delim + s,n,1) = @chr_delim
OPTION (MAXRECURSION 0);

答案 2 :(得分:1)

我知道这个问题适用于SQL Server 2008,但事情发展如此,从 SQL Server 2016 开始,你可以做到这一点

DECLARE @string varchar(100) = 'Richard, Mike, Mark'

SELECT value FROM string_split(@string, ',')

答案 3 :(得分:0)

CREATE FUNCTION [dbo].[Split]
(
    @List varchar(max),
    @SplitOn nvarchar(5)
)
RETURNS @RtnValue table
(
    Id int identity(1,1),
    Value nvarchar(max)
)
AS
BEGIN
    While (Charindex(@SplitOn,@List)>0)
    Begin

        Insert Into @RtnValue (value)
        Select
        Value = ltrim(rtrim(Substring(@List,1,Charindex(@SplitOn,@List)-1)))

        Set @List = Substring(@List,Charindex(@SplitOn,@List)+len(@SplitOn),len(@List))
    End

    Insert Into @RtnValue (Value)
    Select Value = ltrim(rtrim(@List))

Return
END

创建上述功能并执行下方查询以获得结果。

Select * From Dbo.Split('Stack Over Flow',' ')

建议:使用分隔符获取分割值。它更好。 (例如'Stack,Over,Flow')

答案 4 :(得分:-7)

硬。真的很难 - String Manipulation和SQL ... BAD组合。 C#/ .NET用于存储过程是一种方式,可以返回一个表定义的类型(表),每行一个项目。