我正在处理SQL Server的正题,该问题正在拆分字符串。我想实现一个将字符串拆分为数组的函数:
Declare @SQL as varchar(4000)
Set @SQL='3454545,222,555'
Print @SQL
...我必须要做的,我有一个数组,其中有:
total splitCounter=3
Arr(0)='3454545'
Arr(1)='222'
Arr(2)='555'
下面的分割功能不满足我的上述需要,将一个字符串分割成一个数组。
CREATE FUNCTION [dbo].[SplitString]
(
@String varchar(max)
, @Separator varchar(10)
)
RETURNS TABLE
AS RETURN
(
WITH
Split AS (
SELECT
LEFT(@String, CHARINDEX(@Separator, @String, 0) - 1) AS StringPart
, RIGHT(@String, LEN(@String) - CHARINDEX(@Separator, @String, 0)) AS RemainingString
UNION ALL
SELECT
CASE
WHEN CHARINDEX(@Separator, Split.RemainingString, 0) = 0 THEN Split.RemainingString
ELSE LEFT(Split.RemainingString, CHARINDEX(@Separator, Split.RemainingString, 0) - 1)
END AS StringPart
, CASE
WHEN CHARINDEX(@Separator, Split.RemainingString, 0) = 0 THEN ''
ELSE RIGHT(Split.RemainingString, LEN(Split.RemainingString) - CHARINDEX(@Separator, Split.RemainingString, 0))
END AS RemainingString
FROM
Split
WHERE
Split.RemainingString <> ''
)
SELECT
StringPart
FROM
Split
)
如果有任何疑问,请先询问,谢谢。任何类型的建议都将被接受。
-输入
SELECT * FROM SplitStringShamim('A,B,C,DDDDDD,EEE,FF,AAAAAAA', ',')
create FUNCTION [dbo].[SplitStringShamim]
(
@String varchar(max)
, @Separator varchar(10)
)
RETURNS @DataSource TABLE
(
[ID] TINYINT IDENTITY(1,1)
,[Value] NVARCHAR(128)
)
AS
begin
DECLARE @Value NVARCHAR(MAX) = @String
DECLARE @XML xml = N'<r><![CDATA[' + REPLACE(@Value, @Separator, ']]></r><r><![CDATA[') + ']]></r>'
INSERT INTO @DataSource ([Value])
SELECT RTRIM(LTRIM(T.c.value('.', 'NVARCHAR(128)')))
FROM @xml.nodes('//r') T(c)
return
end
从Here开始的分割功能
CREATE FUNCTION [dbo].[fnSplitString]
(
@string NVARCHAR(MAX),
@delimiter CHAR(1)
)
RETURNS @output TABLE(splitdata NVARCHAR(MAX)
)
BEGIN
DECLARE @start INT, @end INT
SELECT @start = 1, @end = CHARINDEX(@delimiter, @string)
WHILE @start < LEN(@string) + 1 BEGIN
IF @end = 0
SET @end = LEN(@string) + 1
INSERT INTO @output (splitdata)
VALUES(SUBSTRING(@string, @start, @end - @start))
SET @start = @end + 1
SET @end = CHARINDEX(@delimiter, @string, @start)
END
RETURN
END
从功能中选择:
select * FROM dbo.fnSplitString('3454545,222,555', ',')
返回
splitdata
--------
3454545
222
555
然后使用光标或while循环将每个人分配给一个变量。一个表本质上已经是一个数组。
我使用了一些类似的东西,但是我放进了一张桌子,所以我也喜欢这样:
-----------------------------------6----3---4----3---3----4------------------------------------------
declare @cadena varchar(max) = '112331,211,1233,124,533,6341';
declare @delimitador as char(1) = ',';
declare @final as int = len(@cadena), @longitudMinima as int = len(reverse(substring(reverse(@cadena),0,charindex(@delimitador,reverse(@cadena)))));
declare @output as table(splits varchar(10));
/*si no tiene delimitador no tiene sentido, es solo un elemento*/
if charindex(@delimitador,@cadena)>0
begin
print 'la cadena tiene' + cast(@final as varchar(10)) + ',deberá llegar a:' + cast(@longitudMinima as varchar(10));
/*se manda hasta la longitud del ultimo elemento*/
while @final<>@longitudMinima
begin
/*mientras se pueda parsear con substring*/
if charindex(@delimitador,@cadena)>0
begin
/*se obtiene elemento de la cadena y la longitud desciende pues la cadena se reduce
*cuando se quita este elemento en el substring*/
declare @element as varchar(100) = substring(@cadena,1,charindex(@delimitador,@cadena)-1);
set @final = len(@cadena);
set @cadena = substring(@cadena,charindex(@delimitador,@cadena)+1,@final);
print 'agregando elemento:' + @element + '--cadena:' + @cadena + '--final:' + cast(@final as varchar(10));
end
else
begin
set @final = len(@cadena);
print 'terminando en elemento:' + @cadena + '--cadena:' + @cadena + '--final:' + cast(@final as varchar(10));
end
end
end
else
begin
print 'elemento:' + @cadena + '--cadena:' + @cadena;
end
创建函数,我这样做
create function [dbo].[returnTableFromString](
@cadena as varchar(max),
@delimitador as char(1)
)
returns @output table(splits varchar(100))
begin
declare @final as int = len(@cadena), @longitudMinima as int = len(reverse(substring(reverse(@cadena),0,charindex(@delimitador,reverse(@cadena)))));
/*si no tiene delimitador no tiene sentido, es solo un elemento*/
if charindex(@delimitador,@cadena)>0
begin
--print 'la cadena tiene' + cast(@final as varchar(10)) + ',deberá llegar a:' + cast(@longitudMinima as varchar(10));
/*se manda hasta la longitud del ultimo elemento*/
while @final<>@longitudMinima
begin
/*mientras se pueda parsear con substring*/
if charindex(@delimitador,@cadena)>0
begin
/*se obtiene elemento de la cadena y la longitud desciende pues la cadena se reduce
*cuando se quita este elemento en el substring*/
declare @element as varchar(100) = substring(@cadena,1,charindex(@delimitador,@cadena)-1);
set @final = len(@cadena);
set @cadena = substring(@cadena,charindex(@delimitador,@cadena)+1,@final);
insert into @output values (@element)
end
else
begin
set @final = len(@cadena);
insert into @output values (@cadena)
end
end
end
else
begin
insert into @output values (@cadena);
end
return
end