Split string into individual characters using TSQL Script

Rajnilari2015
Posted by Rajnilari2015 under Sql Server category on | Points: 40 | Views : 211
DECLARE @String VARCHAR(MAX)
SET @String = 'hello!'

;WITH numberCTE AS
(
SELECT 1 AS Rn
UNION ALL
SELECT Rn+1 FROM numberCTE WHERE Rn<LEN(@String)
)
SELECT Characters = SUBSTRING(@String,rn,1)
FROM numberCTE

/*
Characters
------------
h
e
l
l
o
!
*/

Comments or Responses

Login to post response