我在数据库中有一个逗号分隔的字符串,例如'apple,banana,pineapple,grapes'。我需要根据逗号拆分这个字符串并进行迭代。由于Sql Server没有内置函数,因此是否有一种有效的方法可以实现这个目标。
我在数据库中有一个逗号分隔的字符串,例如'apple,banana,pineapple,grapes'。我需要根据逗号拆分这个字符串并进行迭代。由于Sql Server没有内置函数,因此是否有一种有效的方法可以实现这个目标。
尝试这个函数
CREATE FUNCTION [dbo].[func_Split]
(
@DelimitedString varchar(8000),
@Delimiter varchar(100)
)
RETURNS @tblArray TABLE
(
ElementID int IDENTITY(1,1), -- Array index
Element varchar(1000) -- Array element contents
)
AS
BEGIN
-- Local Variable Declarations
-- ---------------------------
DECLARE @Index smallint,
@Start smallint,
@DelSize smallint
SET @DelSize = LEN(@Delimiter)
-- Loop through source string and add elements to destination table array
-- ----------------------------------------------------------------------
WHILE LEN(@DelimitedString) > 0
BEGIN
SET @Index = CHARINDEX(@Delimiter, @DelimitedString)
IF @Index = 0
BEGIN
INSERT INTO
@tblArray
(Element)
VALUES
(LTRIM(RTRIM(@DelimitedString)))
BREAK
END
ELSE
BEGIN
INSERT INTO
@tblArray
(Element)
VALUES
(LTRIM(RTRIM(SUBSTRING(@DelimitedString, 1,@Index - 1))))
SET @Start = @Index + @DelSize
SET @DelimitedString = SUBSTRING(@DelimitedString, @Start , LEN(@DelimitedString) - @Start + 1)
END
END
RETURN
END
使用示例 - 只需将逗号分隔的字符串和所需的分隔符传递给该函数即可。
DECLARE @SQLStr varchar(100)
SELECT @SQLStr = 'Mickey Mouse, Goofy, Donald Duck, Pluto, Minnie Mouse'
SELECT
*
FROM
dbo.func_split(@SQLStr, ',')
结果会像这样:
SET @DelSize = LEN(@Delimiter + 'x') - 1
来处理带有尾随空格的分隔符,例如 ' '
和 ', '
。 - Chry Cheng...... 因为在 SQL Server 中没有内置函数 ......
您提问时这是正确的,但 SQL Server 2016 引入了 STRING_SPLIT
。
所以您可以直接使用该函数。
SELECT value
FROM STRING_SPLIT ('apple,banana,pineapple,grapes', ',')
有一些限制(只接受单个字符的分隔符,缺乏任何列以指示拆分索引最为引人注目)。各种限制以及一些性能测试的有希望的结果在Aaron Bertrand的这篇博客文章中。
<w>
表示单词),来将数据转换为XML格式。create table t(col varchar(255));
insert into t values ('apple,banana,pineapple,grapes');
insert into t values ('car,bike,airplane');
select cast(('<w>' + replace(col, ',', '</w><w>') + '</w>') as xml) as xmlValue
from t
返回的结果
| XMLVALUE |
|--------------------------------------------------------|
| <w>apple</w><w>banana</w><w>pineapple</w><w>grapes</w> |
| <w>car</w><w>bike</w><w>airplane</w> |
select split.xmlTable.value('.', 'varchar(255)') as xmlValue
from (
select cast(('<w>' + replace(col, ',', '</w><w>') + '</w>') as xml) as xmlValue
from t
) as xmlTable
cross apply xmlValue.nodes ('/w') as split(xmlTable);
我有一个使用递归的解决方案如下
Create function split_string(@str as nvarchar(max),@separator as char(1)) returns @myvalues Table (id int identity(1,1),myval nvarchar(100))
as
--Kamel Gazzah
--23/04/2019
begin
with cte as(
select @str [mystr],
cast(1 as int) [Start],
charindex(@separator,@str)as Nd
union all
select substring(@str,nd+1,len(@str)),cast(Nd+1 as int),charindex(@separator,@str,Nd+1) from cte
where nd>0
)
insert into @myvalues(myval)
select case when nd>0 then substring(@str,start,Nd-start)
else substring(@str,start,len(@str)) end [splitted]
from cte OPTION (MAXRECURSION 1000);
return ;
end;
select * from split_string('apple,banana,pineapple,grapes',',')