如何返回字符串B ,其中包含字符串中每个字符的n个重复变量A 其中变量是字符串中字符的位置
例如 :
因此变量每次重复都会改变
我需要这个用于在Sql中进行计算。
先谢谢
答案 0 :(得分:1)
试试这个..
DECLARE @STR VARCHAR(10)='HELLO',
@Index int=1,
@TotalLength int,
@NewStr varchar(20)='';
SET @TotalLength = LEN(@STR);
WHILE (@Index < @TotalLength+1)
BEGIN
SET @NewStr= @NewStr + (SUBSTRING(@STR,@Index,1)+ CAST(@Index as varchar(2)));
SET @Index=@Index+1;
END
SELECT @NewStr;
答案 1 :(得分:1)
对于Oracle(未指定DMBS):
SELECT
LISTAGG(to_char(strings),'') WITHIN GROUP (ORDER BY rowno) FROM
(
SELECT REGEXP_SUBSTR(regexp_replace('STRINGABCXYZ', '(.)', '\1,' ), '[^,]+', 1, lvl)
|| row_number() OVER (ORDER BY NULL) as strings
,row_number() OVER (ORDER BY NULL) AS rowno
FROM DUAL,
(SELECT LEVEL lvl
,regexp_replace('STRINGABCXYZ', '(.)', '\1,' ) as text
FROM DUAL
CONNECT BY LEVEL <= LENGTH(regexp_replace('STRINGABCXYZ', '(.)', '\1,' ))
- LENGTH(REPLACE(regexp_replace('STRINGABCXYZ', '(.)', '\1,' ),',')))
) innerquery