超智能字符分割

suyiming 2007-10-23 04:52:42
[code=SQL]现在用存储过程参数带过来的字符为"csdn是最大的程序员大本营" 
怎样把它们一个一个拆分开来成这样?
select * from csdn
where
name like '%csdn% 'or name like '%是% 'or name like '%最% 'or name like '%大% 'or name like '%的% 'or name like '%程% 'or name like '%序% 'or name like '%大% 'or name like '%本% ' or name like '%营% '
SQL codeCREATE FUNCTION fn_Split(@sText varchar(8000), @sDelim varchar(20) = ' ')
RETURNS @retArray TABLE (idx smallint Primary Key, value varchar(8000))
AS
BEGIN
DECLARE @idx smallint,
@value varchar(8000),
@bcontinue bit,
@iStrike smallint,
@iDelimlength tinyint
IF @sDelim = 'Space'
BEGIN
SET @sDelim = ' '
END
SET @idx = 0
SET @sText = LTrim(RTrim(@sText))
SET @iDelimlength = DATALENGTH(@sDelim)
SET @bcontinue = 1
IF NOT ((@iDelimlength = 0) or (@sDelim = 'Empty'))
BEGIN
WHILE @bcontinue = 1
BEGIN
--If you can find the delimiter in the text, retrieve the first element and
--insert it with its index into the return table.

IF CHARINDEX(@sDelim, @sText)>0
BEGIN
SET @value = SUBSTRING(@sText,1, CHARINDEX(@sDelim,@sText)-1)
BEGIN
INSERT @retArray (idx, value)
VALUES (@idx, @value)
END

--Trim the element and its delimiter from the front of the string.
--Increment the index and loop.
SET @iStrike = DATALENGTH(@value) + @iDelimlength
SET @idx = @idx + 1
SET @sText = LTrim(Right(@sText,DATALENGTH(@sText) - @iStrike))

END
ELSE
BEGIN
--If you can抰 find the delimiter in the text, @sText is the last value in
--@retArray.
SET @value = @sText
BEGIN
INSERT @retArray (idx, value)
VALUES (@idx, @value)
END
--Exit the WHILE loop.
SET @bcontinue = 0
END
END
END
ELSE
BEGIN
WHILE @bcontinue=1
BEGIN
--If the delimiter is an empty string, check for remaining text
--instead of a delimiter. Insert the first character into the
--retArray table. Trim the character from the front of the string.
--Increment the index and loop.
IF DATALENGTH(@sText)>1
BEGIN
SET @value = SUBSTRING(@sText,1,1)
BEGIN
INSERT @retArray (idx, value)
VALUES (@idx, @value)
END
SET @idx = @idx+1
SET @sText = SUBSTRING(@sText,2,DATALENGTH(@sText)-1)

END
ELSE
BEGIN
--One character remains.
--Insert the character, and exit the WHILE loop.
INSERT @retArray (idx, value)
VALUES (@idx, @sText)
SET @bcontinue = 0
END
END
END
RETURN
END

下面是调用示例:

declare @no char(100)
set @no='abc,cdesefd,efddc,mgns,aa'
select * from fn_Split(@no,',')



智能分割字符函数
[/code]
...全文
97 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
suyiming 2007-11-01
  • 打赏
  • 举报
回复
c#我没有拆分过哦 楼上的兄弟可有例子?
suyiming 2007-10-31
  • 打赏
  • 举报
回复
c#我没有拆分过哦 楼上的兄弟可有例子?
ikiss 2007-10-30
  • 打赏
  • 举报
回复
老兄,把where后边的那一行都作为参数吧,然后用C#去拆分字符串,如果是英文就以 空格 拆分,如果是汉字就逐字拆分
就是类似这种
CREATA procedure sp_csdn @like varchar(100)
as
exec('select * from where name like '+@like)
suyiming 2007-10-30
  • 打赏
  • 举报
回复
这个终于实现了以前 like搜索不出来的 东西
例如:广州亚洲国际大酒店
关键字 广州国际
旧方法: select * from hotel where hotelname like'%广州国际%' 查询出不来
新方法:
用那个分割函数 把关键字插进一个新的表 然后 进行关键查询比较
CREATE    proc UP_Getname4
@Name1 nvarchar(100),
@Name2 nvarchar(100),
@Name3 nvarchar(100),
@Name4 nvarchar(100)
as
select top 500 *
from V_SearchHotel
where
hotelname like ''+'%'+@Name1+'%'+''
and hotelname like ''+'%'+@Name2+'%'+''
and hotelname like ''+'%'+@Name3+'%'+''
and hotelname like ''+'%'+@Name4+'%'+''
suyiming 2007-10-24
  • 打赏
  • 举报
回复
动态生成词库
insert into select
Yang__XZ 2007-10-23
  • 打赏
  • 举报
回复
老大你这个不是动态的啊,实现动态的要怎么写啊?
westart 2007-10-23
  • 打赏
  • 举报
回复
老兄,这个要建词库的,包括google和baidu,没有词库如何分词啊?电脑不是神啊
qufo 2007-10-23
  • 打赏
  • 举报
回复
split 是干嘛用的?
conannb 2007-10-23
  • 打赏
  • 举报
回复
这是sql?
suyiming 2007-10-23
  • 打赏
  • 举报
回复
oh shift
suyiming 2007-10-23
  • 打赏
  • 举报
回复
怎没人知道

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧