我把一段文本放在数据表的一个字段里,这段文本是连续的,没有空格等分隔符,如何查询这个文本里某个字符串,并统计这个串在这段文本中出现过几次。有点类似文本编辑软件的查找搜索功能。我建立了全文搜索,但只能查词,并返回这个词所在的记录,不能返回这个记录有多少这样的字符窜,如何不分匹配?

解决方案 »

  1.   

    /*
    统计一个表中某个字符出现最多的字母
    */
    --创建数据测试环境
    create table #tb(a varchar(200))
    insert into #tb
    select 'abcdasdlfjaslk;dfqwoieurwhft'
    union all select 'a;sldkjfal;sopqwhtdlkdafrfgsgasdfh'
    union all select 'asldfkworufgsea87y9oqwpe tchgwccmnqnw3 '--为字符分拆准备临时表,top 200 是根据要分拆的字符串的最大长度而定的,
    --在测试中,因为a字段的最大为200,故这里用 top 200
    select top 200 id=identity(int,1,1)
    into #aa from
    (select top 100 id from syscolumns) a
    ,(select top 100 id from syscolumns) b
    ,(select top 100 id from syscolumns) c--得到结果
    select top 1 substring(a,b.id,1)
    from #tb a,#aa b 
    where substring(a.a,b.id,1)<>''
    group by substring(a.a,b.id,1)
    order by sum(1) descdrop table #tb,#aa
      

  2.   

    上面的我觉得太麻烦,我已经找到答案。打开并使用数据库的CLR 功能,直接用。net的正则表达式类创建数据库自定义函数。