Showing posts with label How to display a string vertically in SQL Server?. Show all posts
Showing posts with label How to display a string vertically in SQL Server?. Show all posts

How to display a string vertically in SQL Server?

Below I have mentioned 2 queries using which you can display a string vertically:

Query 1: Using While loop:

DECLARE @string VARCHAR(256) = 'welcome'
DECLARE @cnt INT = 0;

WHILE(@cnt < len(@string))
BEGIN
    SET @cnt = @cnt + 1;
    PRINT SUBSTRING ( @string ,@cnt , 1 )
END;


Query 2: Using CTE(Common Table Expressions):

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


Conclusion:

Above queries gives us a way how we can display a string vertically in Microsoft SQL Server.