I have data in a table which looks like this (worth noting its not CSV seperated)
It needs to be split in to single chars
Data
abcde
want to convert it to this
Data
a
b
d
c
e
I have looked on the internet but have not found the answer
CREATE FUNCTION dbo.SplitLetters
(
@s NVARCHAR(MAX)
)
RETURNS @t TABLE
(
[order] INT,
[letter] NCHAR(1)
)
AS
BEGIN
DECLARE @i INT;
SET @i = 1;
WHILE @i <= LEN(@s)
BEGIN
INSERT @t SELECT @i, SUBSTRING(@s, @i, 1);
SET @i = @i + 1;
END
RETURN;
END
GO
SELECT [letter]
FROM dbo.SplitLetters(N'abcdefgh12345 6 7')
ORDER BY [order];
Previous post that solves the problem: TSQL UDF To Split String Every 8 Characters
Pass a value of 1 to @length.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With