MsSQL   发布时间:2022-05-16  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了sql-server-2005 – 在Sql Server 2005中将字符串拆分为单个字符大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
嗨,我有一个输入
ID  data
1   Hello
2   sql

期望的输出是

ID  RowID  Chars
1    1     H
1    2     e
1    3     l
1    4     l
1    5     o
2    1     s
2    2     q
2    3     l

我的方法到目前为止

Declare @t table(ID  int idENTITY,data varchar(maX))
Insert into @t SELEct 'Hello' union all SELEct 'sql'
--SELEct * from @t
;With CteMaxlen As(
SELEct MaxLength = max(len(data)) from @t),Num_Cte AS
(     
      SELECT 1 AS rn
      union all
      SELECT rn +1 AS rn 
      FROM Num_Cte 
      WHERE rn <(SELEct MaxLength from CteMaxlen)
)
-- shred into individual characters,Get_Individual_Chars_Cte AS
( 
      SELECT  
            ID,Row_ID =ROW_numbER() Over(PARTITION by ID Order by ID),chars               
      FROM @t,Num_Cte
      CROSS APPLY( SELECT SUBStriNG((SELEct data from  @t),rn,1)  AS chars) SplittedChars       
)

SELEct * from Get_Individual_Chars_Cte

查询根本不起作用

编辑:

我找到了答案

;with Get_Individual_Chars_Cte AS
( 
   SELECT 
        ID,SUBStriNG(Data,number,1) AS [Char]--,FROM @t  
INNER JOIN master.dbo.spt_values ON
 number betweeN 1 AND LEN(Data)
 AND type='P'

)

SELEct * from Get_Individual_Chars_Cte

需要帮助

解决方法

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

大佬总结

以上是大佬教程为你收集整理的sql-server-2005 – 在Sql Server 2005中将字符串拆分为单个字符全部内容,希望文章能够帮你解决sql-server-2005 – 在Sql Server 2005中将字符串拆分为单个字符所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: