1

SQL Server 中是否有将长文本拆分为多行的函数?

假设我有 1,000 个字符,需要将文本拆分为多行,每行最多 80 个字符,但您只能在单词中间的空格处拆分?

谢谢。

4

2 回答 2

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别名中。

于 2013-09-04T02:19:42.737 回答
0

没有内置的拆分功能,但您可以使用多个子字符串/字符索引,例如:

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 小提琴

或者搜索/创建一个 UDF,似乎有很多。

可以在 cte 中使用 substring/charindex 方法并旋转结果。

于 2013-09-03T22:34:43.457 回答