create table #text_test_a(symbol varchar(20))
--临时表说明:存放在content中出现过的股票代码
truncate table #text_test_a
/*****创建游标从content列中取出股票代码******/
--由于在一行中可以出现多个股票代码,所以循环
--从每一行中取出所有股票代码
declare cur cursor
for select cast(content as nvarchar(4000)) from news
declare @content nvarchar(4000)
open cur
fetch next from cur into @content
while @@fetch_status=0
begin
while charindex('(',@content,0)>0
--如果行中存在括号则进入以下循环
begin
if isnumeric(substring(@content,charindex('(',@content,0)+1,1))=0 and isnumeric(substring(@content,charindex('(',@content,0)+3,1))=1
--由于括号中可能会出现“沪:600001”之类的股票代码,所以去除“沪:”
begin
set @content=stuff(@content,charindex('(',@content,0)+1,2,'')
end
if isnumeric(substring(@content,charindex('(',@content,0)+1,1))=0 and isnumeric(substring(@content,charindex('(',@content,0)+4,1))=1
--由于括号中可能会出现“沪A:600001”之类的股票代码,所以去除“沪A:”
begin
set @content=stuff(@content,charindex('(',@content,0)+1,3,'')
end
if isnumeric(substring(@content,charindex('(',@content,0)+1,6))=0
--如果括号中不为股票代码(即数字,.HK的股票代码也去掉),则忽略此括号,查找行中的下一括号的内容
begin
set @content=stuff(@content,1,charindex('(',@content,0)+1,'')
end
else
--如果是股票代码,则将其放入到临时表中,然后查找行中下一括号中的内容
begin
insert into #text_test_a
select substring(@content,charindex('(',@content,0)+1,6)
set @content=stuff(@content,1,charindex('(',@content,0)+1,'')
--select @content
end
end
fetch next from cur into @content
end
close cur
deallocate cur
/******统计每支股票出现的次数******/