我的字符串(VARCHAR(255)
)只包含0或1
我需要搜索所有位置并将它们作为逗号分隔的字符串返回。
我使用https://dba.stackexchange.com/questions/41961/how-to-find-all-positions-of-a-string-within-another-string
到目前为止,这是我的代码:
DECLARE @TERM VARCHAR(5);
SET @TERM = '1';
DECLARE @STRING VARCHAR(255);
SET @STRING = '101011011000000000000000000000000000000000000000';
DECLARE @RESULT VARCHAR(100);
SET @RESULT = '';
SELECT
@RESULT = @RESULT + CAST(X.pos AS VARCHAR(10)) + ','
FROM
( SELECT
pos = Number - LEN(@TERM)
FROM
( SELECT
Number
,Item = LTRIM(RTRIM(SUBSTRING(@STRING, Number, CHARINDEX(@TERM, @STRING + @TERM, Number) - Number)))
FROM
( SELECT ROW_NUMBER () OVER (ORDER BY [object_id]) FROM sys.all_objects
) AS n ( Number )
WHERE
Number > 1
AND Number <= CONVERT(INT, LEN(@STRING))
AND SUBSTRING(@TERM + @STRING, Number, LEN(@TERM)) = @TERM
) AS y
) X;
SELECT
SUBSTRING(@RESULT, 0, LEN(@RESULT));
DECLARE @POS INT;
DECLARE @OLD_POS INT;
DECLARE @POSITIONS VARCHAR(100);
SELECT
@POSITIONS = '';
SELECT
@OLD_POS = 0;
SELECT
@POS = PATINDEX('%1%', @STRING);
WHILE @POS > 0
AND @OLD_POS <> @POS
BEGIN
SELECT
@POSITIONS = @POSITIONS + CAST(@POS AS VARCHAR(2)) + ',';
SELECT
@OLD_POS = @POS;
SELECT
@POS = PATINDEX('%1%', SUBSTRING(@STRING, @POS + 1, LEN(@STRING))) + @POS;
END;
SELECT
LEFT(@POSITIONS, LEN(@POSITIONS) - 1);
我想知道这是否可以更快/更好地完成?我只搜索单个字符位置,我的字符串中只有两个字符(0和1)。
我使用这段代码构建了两个函数,并运行1000条记录并同时得到相同的结果,所以我不知道哪一个更好。
单个记录第二部分的给出CPU并在Profiler中读取等于0,其中第一段代码给我CPU = 16并读取= 17.
我需要得到如下结果:1,3,5,6,8,9
(多次出现时),3
表示单次出现,NONE
如果没有出现。
答案 0 :(得分:3)
一些tally
表和xml
解决方案:
DECLARE @STRING NVARCHAR(100) = '101011011000000000000000000000000000000000000000';
;with cte as(select ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) p
from (values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) t1(n) cross join
(values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) t2(n) cross join
(values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) t3(n))
SELECT STUFF((SELECT ',' + CAST(p AS VARCHAR(100))
FROM cte
WHERE p <= LEN(@STRING) AND SUBSTRING(@STRING, p, 1) = '1'
FOR XML PATH('')), 1, 1, '')
您只需生成1到1000之间的数字(如果字符串的长度更大,则添加更多连接)并使用substring
函数过滤器所需的值。然后是将行连接到逗号分隔值的标准技巧。
旧版本:
;with cte as(SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) p
FROM sys.all_columns a CROSS JOIN sys.all_columns b)
SELECT STUFF((SELECT ',' + CAST(p AS VARCHAR(100))
FROM cte
WHERE p <= LEN(@STRING) AND SUBSTRING(@STRING, p, 1) = '1'
FOR XML PATH('')), 1, 1, '')
这是关于生成范围http://dwaincsql.com/2014/03/27/tally-tables-in-t-sql/
的好文章修改强>
;with cte as(SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) p
FROM (SELECT 1 AS rn UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 ) t1 CROSS JOIN
(SELECT 1 AS rn UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 ) t2 CROSS JOIN
(SELECT 1 AS rn UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 ) t3 CROSS JOIN
(SELECT 1 AS rn UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 ) t4 CROSS JOIN
(SELECT 1 AS rn UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 ) t5 CROSS JOIN
(SELECT 1 AS rn UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 ) t6)
答案 1 :(得分:1)
Giorgi的回答非常聪明,但我更喜欢更老式的方法,更具可读性。我的建议,包括测试用例:
if object_id('UFN_CSVPOSITIONS') is not null
begin
drop function ufn_csvpositions;
end
go
create function dbo.UFN_CSVPOSITIONS
(
@string nvarchar(255)
,@delimiter nvarchar(1) = ','
)
returns nvarchar(255)
as
begin
--given a string that contains ones,
--return a comma-delimited list of the positions of those ones
--example: '1001' returns '1,4'
declare @result nvarchar(255) = '';
declare @i int = 1;
declare @slen int = len(@string);
declare @idx int = 0;
while @i < @slen
begin
set @idx = charindex('1',@string,@i);
if 0 = @idx
begin
set @i = @slen; --no more to be found, break out early
end
else
begin
set @result = @result + @delimiter + convert(nvarchar(3),@idx);
set @i = @idx; --jump ahead
end;
set @i = @i + 1;
end --while
if (0 < len(@result)) and (',' = substring(@result,1,1))
begin
set @result = substring(@result,2,len(@result)-1)
end
return @result;
end
go
--test cases
DECLARE @STRING NVARCHAR(255) = '';
set @string = '101011011000000000000000000000000000000000000000';
print dbo.UFN_CSVPOSITIONS(@string,',');
set @string = null;
print dbo.UFN_CSVPOSITIONS(@string,',');
set @string = '';
print dbo.UFN_CSVPOSITIONS(@string,',');
set @string = '1111111111111111111111111111111111111111111111111';
print dbo.UFN_CSVPOSITIONS(@string,',');
set @string = '0000000000000000000000000000000000000000000000000';
print dbo.UFN_CSVPOSITIONS(@string,',');
--lets try a very large # of test cases, see how fast it comes out
--255 "ones" should be the worst case scenario for performance, so lets run through 50k of those.
--on my laptop, here are test case results:
--all 1s : 13 seconds
--all 0s : 7 seconds
--all nulls: 1 second
declare @testinput nvarchar(255) = replicate('1',255);
declare @iterations int = 50000;
declare @i int = 0;
while @i < @iterations
begin
print dbo.ufn_csvpositions(@testinput,',');
set @i = @i + 1;
end;
--repeat the test using the CTE method.
--the same test cases are as follows on my local:
--all 1s : 18 seconds
--all 0s : 15 seconds
--all NULLs: 1 second
set nocount on;
set @i = 0;
set @iterations = 50000;
declare @result nvarchar(255) = '';
set @testinput = replicate('1',255);
while @i < @iterations
begin
;with cte as(SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) p
FROM sys.all_columns a CROSS JOIN sys.all_columns b)
SELECT @result = STUFF((SELECT ',' + CAST(p AS VARCHAR(100))
FROM cte
WHERE p <= LEN(@testinput) AND SUBSTRING(@testinput, p, 1) = '1'
FOR XML PATH('')), 1, 1, '')
print @result;
set @i = @i + 1;
end;