【问题标题】:How to get first n words from a sql server column that have saved a text?如何从已保存文本的 sql server 列中获取前 n 个单词?
【发布时间】:2014-01-21 05:11:00
【问题描述】:

我认为我的问题很清楚。

我的数据库表中有一个名为Description 的列,类型为nvarchar(MAX)
它保存了一个波斯文本。例如,我想从该文本中获取前 100 个单词。
你的想法是什么?

【问题讨论】:

  • 查看stackoverflow.com/questions/697519/…EDIT 首先用空格分割你的字符串,然后得到前 100 个单词。
  • 它得到前 100 个字母,没有前 100 个单词。
  • 你想要 100 字?所以用空间分割你的领域并获得第一个 100 个世界。
  • 是的。单词用空格隔开。
  • 所以请查看我发布的示例。有一个函数可以使用任何分隔符分割文本,然后返回一个表格。所以这样做并从函数表中获得前 100 名:)

标签: database tsql sql-server-2012


【解决方案1】:

创建这个函数:

create function f_getword
(
@txt varchar(max),
@pos int
)
returns varchar(max)
begin
declare @separators varchar(max) = '%[^a-z]%'
declare @returnword varchar(max)
;with x as
(
select patindex('%[a-z][^a-z]%', @txt + ' ') l, 1 cnt
union all
select patindex('%[a-z][^a-z]%', stuff(@txt + ' ', 1, l, '')) + l, cnt + 1
from x
where cnt < @pos and l is not null
)
select top 1 @returnword = left(@txt, l) from x order by cnt desc
option (MAXRECURSION 0)
return coalesce(@returnword, '')
end

这是一个使用函数的表变量:

declare @numberofwords int = 4
declare @t table(txt varchar(max))
insert @t values('here is a text'),
('here is another text, this part will be omitted'),
(''),
('text')

select dbo.f_getword(txt, @numberofwords)
from @t

结果:

here is a text
here is another text

text

【讨论】:

    【解决方案2】:

    这种方法会用文字破坏你的字符串

    -- ================================================
    Create FUNCTION [dbo].[GetRequiredWordsFromString]
    (
    @string nvarchar(max),
    @wordcount int
    )
    RETURNS nvarchar(max)
    AS
    BEGIN
    
        DECLARE @out nvarchar(max) declare @count int
        SET @out=''  SET @count=0
        DECLARE   @pos        int,
                  @nextpos    int,
                  @valuelen   int
    
     SELECT @pos = 0, @nextpos = 1
    
        WHILE (@nextpos > 0)
       BEGIN
           IF(@count=@wordcount)break;
          SELECT @nextpos = charindex(' ', @string, @pos + 1)
          SELECT @valuelen = CASE WHEN @nextpos > 0
                                  THEN @nextpos
                                  ELSE len(@string) + 1
                             END - @pos - 1
          SET @out+=(convert(nvarchar, substring(@string, @pos + 1, @valuelen)))+' '
          SELECT @count=@count+1
          SELECT @pos = @nextpos
       END
    
        RETURN @out
    
    END
    GO
    

    并在要从中获取 n 个单词的表上运行此查询

       Declare @myDescripition table
    (
    Descp nvarchar(max)
    )
    
    declare @name nvarchar(max)
    declare cur cursor local for select Name from Employees
    open cur
    fetch cur into @name
    while (@@fetch_status=0)
    BEGIN
       insert into @myDescripition 
       select [dbo].[GetRequiredWordsFromString](@name,100)
    fetch cur into @name
    END
    Close cur
    Deallocate cur
    
    select * from @myDescripition
    

    【讨论】:

      【解决方案3】:

      试试这个..

      SubString(Column_Name,Start_Index,Length)
      select SubString(Name,0,100) from Employee
      

      【讨论】:

      • 请注意!我想得到没有字母的单词。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多