SQL Server中是否有将长文本拆分为多行的函数?
假设我有1,000个字符,需要将文本拆分成多行,每行最多80个字符,但是只能在不在单词中间的空格处拆分?
感谢。
答案 0 :(得分:1)
您可以使用递归CTE拆分文本。这是一个例子:
with t as (
select 1 as id, 'abcefghijkl' as line union
select 2, 'zyx'
),
const as (
select 1 as linelen
),
splitlines as (
select id, left(line, linelen) as part, substring(line, linelen + 1, len(line)) as rest
from t cross join const
union all
select id, left(rest, linelen) as part, substring(rest, linelen + 1, len(rest))
from splitlines cross join const
where len(rest) > 0
)
select *
from splitlines;
您的问题不清楚是要拆分表中的单个变量还是列。在任何情况下,值都在t
别名中。行长度在const
别名中。
答案 1 :(得分:0)
没有内置的分割功能,但你可以使用多个子串/ charindex,如:
DECLARE @FieldName VARCHAR(MAX) = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.'
SELECT SUBSTRING(@FieldName,1,CHARINDEX(' ',@FieldName,70)-1)
,SUBSTRING(@FieldName,CHARINDEX(' ',@FieldName,70)+1,CHARINDEX(' ',@FieldName,140)-CHARINDEX(' ',@FieldName,70))
,SUBSTRING(@FieldName,CHARINDEX(' ',@FieldName,140)+1,CHARINDEX(' ',@FieldName,210)-CHARINDEX(' ',@FieldName,140))
演示:SQL Fiddle
或者搜索/创建一个UDF,似乎很多。
可以在cte中使用substring / charindex方法并旋转结果。