【问题标题】:Divide long string into multiple strings SQL将长字符串分割成多个字符串SQL
【发布时间】:2019-03-14 05:09:37
【问题描述】:

所以基本上我希望将一根长字符串切成小块,但要保持单词完整。因此,如果我在JumpColumn列中的FoxTable中有以下句子:

棕色的狐狸跳过了懒惰的狗,然后懒惰的狗跳过了棕色的狐狸

我想在 SQL 中将其拆分为最多 20 个字符(包括空格)。所以结果会是这样的:

the brown fox jumped 
 over the lazy dog
 and then the lazy 
dog jumped over the 
brown fox

但是,请记住,JumpColumn 列中有不同的句子,具有不同的长度和单词。

我的查询现在所做的是使用 SQL-Server 中的 SUBSTRING 函数将其分成不同的行。

DECLARE @string_part1 NVARCHAR(500), 
        @string_part2 NVARCHAR(500), 
        @string_part3 NVARCHAR(500), 
        @string_part4 NVARCHAR(500),
        @string_part5 NVARCHAR(500),
        @LoopCount = 1

WHILE (@LoopCount <= 100)
BEGIN
     SELECT
      string_part1 NVARCHAR(500)  = SUBSTRING(JumpColumn, 0, 20)
     ,string_part2 NVARCHAR(500)  = SUBSTRING(JumpColumn, 20, 20)
     ,string_part3 NVARCHAR(500)  = SUBSTRING(JumpColumn, 40, 20)
     ,string_part4 NVARCHAR(500)  = SUBSTRING(JumpColumn, 60, 20)
     ,string_part5 NVARCHAR(500)  = SUBSTRING(JumpColumn, 80, 20)

FROM FoxTable
WHERE Row = @LoopCount

SET @LoopCount = @LoopCount + 1
END

【问题讨论】:

  • 我会写一个函数,放一个';' (或您要使用的任何其他分隔符)在单词末尾达到 20 个字符,然后使用 string_split 将其拆分为多行
  • 也许您可以尝试使用正则表达式 CLR,然后您将有更多选项来处理字符串

标签: sql sql-server


【解决方案1】:

这会起作用

declare @str nvarchar(300) = 'the brown fox jumped  over the lazy dog and then the lazy dog jumped over the brown fox'
declare @finalstring nvarchar(300) =''
while(1=1)
begin


set @finalstring = @finalstring + substring(@str,1,charindex(' ',@str,20)) + ';'
set @str = substring(@str,1+len(substring(@str,1,charindex(' ',@str,20))),len(@str))
if(substring(@str,1,charindex(' ',@str,20)) ='') 
begin
set @finalstring = @finalstring + @str
break
end

end

select ltrim(value) from string_split(@finalstring,';')

我知道不是很整洁,但它可以完成工作。

【讨论】:

    【解决方案2】:

    从 SQL Server 2016 开始,您可以使用 string_split 将句子拆分为单词(在 SQL Server 2016 之前,您可以使用可以在网络上找到的类似功能):

    declare @string         nvarchar(max) = 'the brown fox jumped over the lazy dog and then the lazy dog jumped over the brown fox'
    declare @tmp            table (row_num int identity(1,1), word nvarchar(20), word_length int)
    declare @total          int  
    declare @counter        int = 1 
    declare @current_lenght int = 0
    declare @current_string nvarchar(20) = '' 
    declare @next_string    nvarchar(20) = ''
    declare @res            table (words nvarchar(20))
    
    insert into @tmp
    select value, len(value) 
    from string_split(@string, ' ')
    
    select @total = count(*) from @tmp
    
    while @counter <= @total
        begin
            select @next_string = word 
            from @tmp
            where row_num = @counter
    
            if len(case when @counter = 1 then '' else @current_string + ' ' end + @next_string) > 20
                begin
                    insert into @res values (@current_string)
                    set @current_string = @next_string
                end
            else
                set @current_string = case when @counter = 1 then '' else @current_string + ' ' end + @next_string
            set @counter = @counter + 1
        end
    insert into @res values (@current_string)
    
    select * from @res
    

    结果:

    【讨论】:

      猜你喜欢
      • 2011-09-01
      • 1970-01-01
      • 2020-12-19
      • 1970-01-01
      • 2017-06-18
      • 1970-01-01
      • 1970-01-01
      • 2020-04-23
      • 1970-01-01
      相关资源
      最近更新 更多