如何从已保存文本的sql server列中获取前n个单词?

时间:2014-01-03 13:54:48

标签: database tsql sql-server-2012

我认为我的问题很明确。

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

3 个答案:

答案 0 :(得分:0)

此方法会破坏您的字符串

-- ================================================
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

答案 1 :(得分:0)

创建此功能:

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 :(得分:-1)

试试这个..

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