统计每个单词在字符串中出现的次数

mashengxuucom 2011-08-06 07:57:04
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[fnWordInStrCount]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[fnWordInStrCount]
GO

CREATE FUNCTION [fnWordInStrCount](@Str VARCHAR(MAX),@Separator VARCHAR(3))
RETURNS @WordInStrCount TABLE
(
sWord VARCHAR(50)
,iCount INT
)
WITH ENCRYPTION
AS
BEGIN
DECLARE @Len INT,@iIndex INT,@iCount INT
DECLARE @sWord VARCHAR(50)
SET @iCount = 0
SET @Len = LEN(@Str)
SET @iIndex = 1
WHILE @Len > 0
BEGIN
--取出第一个单词(除分隔符)
SET @sWord = LEFT(@Str + @Separator,CHARINDEX(@Separator,@Str + @Separator,1)-LEN(@Separator))
WHILE @iIndex < @Len
BEGIN
--该单词如果等于取出的单词,@iCount + 1
IF @sWord = SUBSTRING(@Str + @Separator,@iIndex,CHARINDEX(@Separator,@Str + @Separator,@iIndex) - LEN(@Separator))
SET @iCount = @iCount + 1
--@iIndex 取单词的位置
SET @iIndex = @iIndex + LEN(@sWord + @Separator)
END
INSERT INTO @WordInStrCount(sWord,iCount)
VALUES(@sWord,@iCount)
SET @iCount = 0
SET @iIndex = 1
SET @Str = REPLACE(@Str + @Separator,@sWord + @Separator,'')
SET @Len = LEN(@Str)
END
RETURN
END

GO
SELECT * FROM [dbo].[fnWordInStrCount]('HOW,OLD,ARE,YOU,HOW,DO,YOU,DO',',')

统计每个单词在字符串中出现的次数
请问各位这个哪出错了?
...全文
479 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
叶子 2011-08-07
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 acherat 的回复:]
老王大哥用两个表变量!
[/Quote]

create function [dbo].[m_split5](@c varchar(2000),@split varchar(2))
returns @d table(col varchar(200),icount int)
as
begin
while(charindex(@split,@c)<>0)
begin
insert @d(col) values (substring(@c,1,charindex(@split,@c)-1))
set @c = stuff(@c,1,charindex(@split,@c),'')
end
insert @d(col) values (@c)
update @d set icount= (select count(*) from @d b where col=a.col) from @d a
return
end

select distinct * from dbo.m_split5(replace('HOW,OLD,ARE;YOU;HOW,DO,YOU,DO',',',';'),';')
火光闪耀 2011-08-07
  • 打赏
  • 举报
回复
好难的SQL啊,看不懂。
AcHerat 元老 2011-08-07
  • 打赏
  • 举报
回复
老王大哥用两个表变量!
AcHerat 元老 2011-08-07
  • 打赏
  • 举报
回复

create function [fnwordinstrcount](@str varchar(max),@separator varchar(3))
returns @wordinstrcount table
(
sword varchar(50),
icount int
)
with encryption
as
begin
declare @iindex int
declare @sword varchar(50)
set @str = @str + @separator
set @iindex = charindex(@separator,@str)
while @iindex > 0
begin
set @sword = left(@str,@iindex - 1)
if exists (select 1 from @wordinstrcount where sword = @sword)
update @wordinstrcount set icount = icount + 1 where sword = @sword
else
insert into @wordinstrcount select @sword,1
set @str = right(@str,len(@str)-@iindex)
set @iindex = charindex(@separator,@str)
end
return
end
go

select * from [dbo].[fnwordinstrcount]('how,old,are,you,how,do,you,do',',')

drop function [fnwordinstrcount]

/*******************

sword icount
-------------------------------------------------- -----------
how 2
old 1
are 1
you 2
do 2

(5 行受影响)
叶子 2011-08-07
  • 打赏
  • 举报
回复

/*按照符号分割字符串*/
create function [dbo].[m_split3](@c varchar(2000),@split varchar(2))
returns @d table(col varchar(200),icount int)
as
begin
declare @t table(col varchar(200))
while(charindex(@split,@c)<>0)
begin
insert @t(col) values (substring(@c,1,charindex(@split,@c)-1))
set @c = stuff(@c,1,charindex(@split,@c),'')
-- SET @c = substring(@c,charindex(' ',@c)+1,len(@c))
end
insert @t(col) values (@c)
insert @d select col,count(1) from @t group by col order by 2 desc
return

end

select * from dbo.m_split3(replace('HOW,OLD,ARE;YOU;HOW,DO,YOU,DO',',',';'),';')

/*
col icount
--- ----
DO 2
HOW 2
YOU 2
ARE 1
OLD 1
*/
oO寒枫Oo 2011-08-07
  • 打赏
  • 举报
回复

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[fnWordInStrCount]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[fnWordInStrCount]
GO

CREATE FUNCTION [fnWordInStrCount](@Str VARCHAR(MAX),@Separator VARCHAR(3))
RETURNS @WordInStrCount TABLE
(
sWord VARCHAR(50)
,iCount INT
)
WITH ENCRYPTION
AS
BEGIN
DECLARE @Len INT,@iIndex INT,@iCount INT
DECLARE @sWord VARCHAR(50)
SET @iCount = 0
SET @Len = LEN(@Str)
WHILE @Len > 0
BEGIN
----分隔符在前面就去掉
WHILE (left(@Str,len(@Separator))=@Separator)
begin
set @Str=right(@Str,len(@str)-len(@Separator))
end
--取出第一个单词 并且算出该单词的个数
SET @sWord = LEFT(@Str,CHARINDEX(@Separator,@Str)-1)
select @iCount=(len(@Str)-len(replace(@Str,@sWord,'')))/len(@sWord)
--插入该单词记录
INSERT INTO @WordInStrCount(sWord,iCount)
VALUES(@sWord,@iCount)
---重建@Str
SET @Str = REPLACE(@Str,@sWord,'')
SET @Len = LEN(@Str)
if @Str is null
set @Len=0
END
RETURN
END
GO
SELECT * FROM [dbo].[fnWordInStrCount]('HOW,;OLD,;ARE,;YOU,;HOW,;DO,;YOU,;DO',',;')
mashengxuucom 2011-08-07
  • 打赏
  • 举报
回复
这个答案是做出来了,但貌似有点不符合我的要求:
出现的次数不是通过select算出来的,是放在函数里求出来的,通过count来计算还是不太方便

如果分隔符多于一个这个就不行了...

这个还不是通用型 的
oO寒枫Oo 2011-08-07
  • 打赏
  • 举报
回复

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[fnWordInStrCount]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[fnWordInStrCount]
GO

CREATE FUNCTION [fnWordInStrCount](@Str VARCHAR(MAX),@Separator VARCHAR(3))
RETURNS @WordInStrCount TABLE
(
sWord VARCHAR(50)
,iCount INT
)
WITH ENCRYPTION
AS
BEGIN
DECLARE @Len INT,@iIndex INT,@iCount INT
DECLARE @sWord VARCHAR(50)
SET @iCount = 0
SET @Len = LEN(@Str)
SET @iIndex = 1
WHILE @Len > 0
BEGIN
----分隔符在前面就去掉
WHILE (left(@Str,len(@Separator))=@Separator)
begin
set @Str=right(@Str,len(@str)-len(@Separator))
end
--取出第一个单词 并且算出该单词的个数
SET @sWord = LEFT(@Str,CHARINDEX(@Separator,@Str,1)-LEN(@Separator))
select @iCount=(len(@Str)-len(replace(@Str,@sWord,'')))/len(@sWord)
--插入该单词记录
INSERT INTO @WordInStrCount(sWord,iCount)
VALUES(@sWord,@iCount)
---重建@Str
SET @Str = REPLACE(@Str,@sWord,'')
SET @Len = LEN(@Str)
if @Str is null
set @Len=0
END
RETURN
END
GO
SELECT * FROM [dbo].[fnWordInStrCount]('HOW,OLD,ARE,YOU,HOW,DO,YOU,DO',',')
mashengxuucom 2011-08-07
  • 打赏
  • 举报
回复
GO

CREATE FUNCTION [dbo].[fnWordInStrCount](@Str VARCHAR(MAX),@Separator VARCHAR(3))
RETURNS @fnWordInStrCount TABLE
(
sWord VARCHAR(50)
,iCount INT
)
WITH ENCRYPTION
AS
BEGIN
DECLARE @sWord VARCHAR(50),@iCount INT
SET @Str = @Str + @Separator
WHILE @Str <> ''
BEGIN
SET @sWord = LEFT(@Str,CHARINDEX(@Separator,@Str,1)-1)
IF EXISTS(SELECT 1 FROM @fnWordInStrCount
WHERE sWord = @sWord)
UPDATE @fnWordInStrCount
SET iCount = iCount + 1
WHERE sWord = @sWord
ELSE
INSERT INTO @fnWordInStrCount(sWord,iCount)
VALUES(@sWord,1)
SET @Str = RIGHT(@Str,LEN(@Str)-LEN(@sWord + @Separator))
END
RETURN
END

GO

SELECT * FROM [dbo].[fnWordInStrCount]('HOW; ,DO; ,YOU; ,DO','; ,')


/*
HOW 1
DO 2
YOU 1
*/
这样行了,
谢谢各位啦,
有更好的做法,望各位继续提供
3Q
mashengxuucom 2011-08-07
  • 打赏
  • 举报
回复
lxpbs8851
不符合题意
oO寒枫Oo 2011-08-07
  • 打赏
  • 举报
回复

declare @str varchar(20)
declare @sql varchar(200)
set @sql='1234a7897a'
set @str='a'
select (len(@sql)-len(replace(@sql,@str,'')))/len(@str)
oO寒枫Oo 2011-08-07
  • 打赏
  • 举报
回复

select (len(@sql)-len(replace(@sql,@str,''))) /len(@str) as 个数
oO寒枫Oo 2011-08-07
  • 打赏
  • 举报
回复

declare @str varchar(20)
declare @sql varchar(20)
select len(@sql)-len(replace(@sql,@str,''))/len(@str) as 个数
xyytuo 2011-08-07
  • 打赏
  • 举报
回复
a14504 2011-08-07
  • 打赏
  • 举报
回复
没看懂
叶子 2011-08-06
  • 打赏
  • 举报
回复

/*按照符号分割字符串*/
create function [dbo].[m_split](@c varchar(2000),@split varchar(2))
returns @t table(col varchar(200))
as
begin
while(charindex(@split,@c)<>0)
begin
insert @t(col) values (substring(@c,1,charindex(@split,@c)-1))
set @c = stuff(@c,1,charindex(@split,@c),'')
-- SET @c = substring(@c,charindex(' ',@c)+1,len(@c))
end
insert @t(col) values (@c)
return
end


select col,icount=count(1)
from dbo.m_split('HOW,OLD,ARE,YOU,HOW,DO,YOU,DO',',')
group by col order by 2 desc
/*
col icount
--- ----
DO 2
HOW 2
YOU 2
ARE 1
OLD 1
*/
mashengxuucom 2011-08-06
  • 打赏
  • 举报
回复
??? 什么没加上去呀,不是数据插入表后
INSERT INTO @WordInStrCount(sWord,iCount)
VALUES(@sWord,@iCount)
SET @iCount = 0
SET @iIndex = 1
SET @Str = REPLACE(@Str + @Separator,@sWord + @Separator,'')
SET @Len = LEN(@Str)

这样有错??
chuanzhang5687 2011-08-06
  • 打赏
  • 举报
回复
没加上去
mashengxuucom 2011-08-06
  • 打赏
  • 举报
回复
没看出这有啥错,望批点
chuanzhang5687 2011-08-06
  • 打赏
  • 举报
回复
INSERT INTO @WordInStrCount(sWord,iCount)
VALUES(@sWord,@iCount)
SET @iCount = 0
SET @iIndex = 1
SET @Str = REPLACE(@Str + @Separator,@sWord + @Separator,'')
SET @Len = LEN(@Str)

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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