T-SQL Split Word into characters

前端 未结 5 1171
太阳男子
太阳男子 2020-11-28 12:19

I have searched everywhere and I cannot find this implementation anywhere.

Let\'s say I have the word: QWERTY

I want to obtain this table:

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-28 12:58

    Here you have it:

    create table #words (
      character varchar(1)
    )
    
    declare @test varchar(10)
    select @test = 'QWERTY'
    
    declare @count int, @total int
    select @total = len(@test), @count = 0
    
    while @count <= @total
    begin
      insert into #words select substring(@test, @count, 1)
      select @count = @count + 1
    end
    
    select * from #words
    
    drop table #words
    

提交回复
热议问题