sql字符查询问题?????急急急!!!!!!

shanglibo 2009-07-16 11:29:58
加精
create table w
(
word nvarchar(100)
)

insert into w(word)
select('去')
union all
select('第三方')
union all
select(',')
union all
select('士大夫')
union all
select('girls')
union all
select('<:')

select* from w

declare @t nvarchar(1000)
set @t='我们今天要去的地方要和第三方枯叶,顶替顶替去要夺枯地枯需要地<.'

我想从这句话中找出w表在的关键字并显示出来,包括显示几次。
结果如下:

去 2
第三方 1
, 1


大学看看该如何实现。。
...全文
1600 71 打赏 收藏 转发到动态 举报
写回复
用AI写文章
71 条回复
切换为时间正序
请发表友善的回复…
发表回复
LX171717 2009-07-24
  • 打赏
  • 举报
回复

--没有 上面的人写得简单 贴出来供参考下。
--写的比较多 比较繁琐--
CREATE function KeyNums
(@key nvarchar(50), @t nvarchar(1000) )
returns int
as
begin

declare @times int
select @times=0
while(1=1)
begin
if exists (select 1 where @t like '%'+@key+'%')
begin

select @times=@times+1
select @t=substring(@t,1,(charindex(@key,@t)-1))
+substring(@t,(charindex(@key,@t)+1),(len(@t)-len(@key)))
end
else
begin
break
end
end
return @times
end

---测试-----
declare @t nvarchar(1000)
select @t='我们今天要去的地方要和第三方枯叶,顶替顶替去要夺枯地枯需要地 <:'
select word
,dbo.KeyNums(word,@t)as Nums
from w
zjwzjw20072008 2009-07-23
  • 打赏
  • 举报
回复
真巧妙,学习了.
zjb211434 2009-07-22
  • 打赏
  • 举报
回复
学习。。。
chuandong_1984 2009-07-22
  • 打赏
  • 举报
回复
学习了
ljsheng 2009-07-21
  • 打赏
  • 举报
回复


看了以后才知道自己对SQL....
xiang13487 2009-07-21
  • 打赏
  • 举报
回复
ding
mjlai 2009-07-21
  • 打赏
  • 举报
回复
yymycabbage 2009-07-21
  • 打赏
  • 举报
回复

insert into w(word)
select('go')
union all
select('third')
union all
select(',')
union all
select('ShiDaFu')
union all
select('girls')
union all
select('<:')
dzhua_isme 2009-07-21
  • 打赏
  • 举报
回复
gs
qingniao8 2009-07-21
  • 打赏
  • 举报
回复
感觉这个效率高,而且简单明了 where @t like '%'+word+'%'
dlchcdcailang 2009-07-21
  • 打赏
  • 举报
回复
看了以后才知道自己对SQL....
手抓饼加辣 2009-07-21
  • 打赏
  • 举报
回复
?
stszd604 2009-07-20
  • 打赏
  • 举报
回复
先顶一下再说
yingzhilian2008 2009-07-20
  • 打赏
  • 举报
回复
Up
caihu2008A 2009-07-20
  • 打赏
  • 举报
回复
顶一下
tangjianft 2009-07-20
  • 打赏
  • 举报
回复
很好很强大!
jiangseraph 2009-07-20
  • 打赏
  • 举报
回复
ding
1001液 2009-07-20
  • 打赏
  • 举报
回复
MARK

严重支持!
Martin 2009-07-20
  • 打赏
  • 举报
回复
恩 学习学习
Judev 2009-07-19
  • 打赏
  • 举报
回复
xue xi
加载更多回复(47)

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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