I need to split a string in a column into one character each into it\'s own column in SQL Server 2012.
Example: if I have a column with \'ABCDE\'
, I need to
One way
declare @str varchar(max) = 'ABCDE'
declare @sql nvarchar(max) = ''
declare @i int = 1
while (@i <= len(@str)) begin
set @sql += case when @i > 1 then ',' else '' end + '''' + substring(@str, @i, 1) + ''''
set @i += 1
end
exec('select ' + @sql)
(If '
can appear as a char you would need to substitute ''
)