我需要将列值拆分为多行,列值不会分层,只是文本。每行不得超过一定数量的字符。
我的桌子只有一栏。
Value
This is the text for test.
现在我希望在一行中有多行的最大长度为5个字符的上述值。
ID Value
1 This (this row has a white space in the end)
2 is the
3 text (this row has a white space in the beginning)
4 for t
5 est. (this row has only 4 character but not more than5 )
答案 0 :(得分:0)
一种方法是使用递归CTE:
with cte as (
select left(col, 5) as char5, substring(col, 6, len(col)) as rest
from table t
union all
select left(rest, 5), substring(rest, 6, len(rest))
from cte
where len(rest) > 0
)
select char5
from cte;
请注意,SQL Server中存在最大递归深度。如果字符串非常长,您可以覆盖默认值。