日期:2014-05-17  浏览次数:20537 次

sql 分词技术
shi yi gong

分词后形成一个如下的table
id one
1 shiyigong
2 shi yigong
3 shi yi-gong
sql 技术

------解决方案--------------------

--創建函數
create  function [dbo].[f_firstposition](@Str varchar(8000),@StrSep varchar(10),@AppPos int)
returns int
begin
    declare @i int
    declare @ii int
    set @Str=rtrim(ltrim(@Str))
    set @i=1
    select @ii=charindex(@StrSep,@Str)
    if @i=@AppPos
        return @ii
    else
    while @AppPos>@i
    begin
        if charindex(@StrSep,right(@Str,len(@Str)-@ii))<>0
                select @ii=charindex(@StrSep,right(@Str,len(@Str)-@ii))+@ii
        else
                set @ii=0
        set @i=@i+1
    end

    return @ii
END
GO

--執行查詢
;WITH a1 (cstr) AS
(
SELECT 'shi yi gong'
)
SELECT REPLACE(cstr,' ','') cstr FROM a1
UNION ALL
SELECT STUFF(cstr,dbo.f_firstposition(cstr,' ',2),1,'') cstr FROM a1
UNION ALL
SELECT STUFF(cstr,dbo.f_firstposition(cstr,' ',2),1,'-') cstr FROM a1

------解决方案--------------------
declare @tb table (cstr varchar(100))
insert @tb select 'shi yi gong'

SELECT REPLACE(cstr,' ','') cstr FROM @tb
UNION ALL
SELECT STUFF(cstr,dbo.f_firstposition(cstr,' ',2),1,'') cstr FROM @tb
UNION ALL
SELECT STUFF(cstr,dbo.f_firstposition(cstr,' ',2),1,'-') cstr FROM @tb