Split a string into individual characters in Sql Server 2005

后端 未结 2 1405
猫巷女王i
猫巷女王i 2020-12-17 23:32

Hi I have an input as

ID  data
1   hello
2   sql

The desired output being

ID  RowID  Chars
1    1     H
1    2     e
1            


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-18 00:09

    ;with cte as
    (
      select ID,
             substring(data, 1, 1) as Chars,
             stuff(data, 1, 1, '') as data,
             1 as RowID
      from @t
      union all
      select ID,
             substring(data, 1, 1) as Chars,
             stuff(data, 1, 1, '') as data,
             RowID + 1 as RowID
      from cte
      where len(data) > 0
    )
    select ID, RowID, Chars
    from cte
    order by ID, RowID
    

提交回复
热议问题