我有如下所示的字符串
Declare @string ='welcome'
我想要这样的输出
w
e
l
c
o
m
e
答案 0 :(得分:0)
DECLARE @string VARCHAR(256) = 'welcome'
DECLARE @cnt INT = 0;
WHILE @cnt < len(@string)
BEGIN
SET @cnt = @cnt + 1;
PRINT SUBSTRING ( @string ,@cnt , 1 )
END;
从本质上讲,您遍历字符串的长度。 您将字符打印在循环索引的位置并打印。
答案 1 :(得分:0)
您可以使用统计表,通常比循环更快,但是您必须自己进行测试:
declare @s varchar(200)
set @s = 'Welcome'
;with t as (
select v
from (values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) t(v)
),
n as (
select row_number() over(order by (select null)) rn
from t t1 cross join t t2
)
select substring(@s, rn, 1)
from n
where rn <= len(@s)
答案 2 :(得分:0)
您可以使用递归CTE。
Declare @string varchar(10) ='welcome'
;with cte as
(
select 1 as i,substring(@string,1,1) as single_char
union all
select i+1 as i,convert(varchar(1),substring(@string,i+1,i+1)) as single_char from cte where len(convert(varchar(1),substring(@string,i+1,i+1)))=1
)
select single_char From cte