【问题标题】:SQL Server : extract hashtags from column of textSQL Server:从文本列中提取主题标签
【发布时间】:2017-05-31 17:32:22
【问题描述】:

我明白这个答案在这里:How to extract hashtags from a string in T-SQL 解释了如何从声明的字符串变量中提取主题标签,但是如何将此操作应用于整个字符串列?

【问题讨论】:

    标签: sql-server tsql


    【解决方案1】:

    使用交叉应用。 只是为了好玩,删除最后的 WHERE,看看会发生什么

    示例

    Declare @YourTable table (ID int,SomeText varchar(max))
    Insert into @YourTable values
     (1, '#want to extract all #hastag out of this string, #delhi #Traffic')
    ,(2, '#bunny #hastag #donetodeath')
    
    Select A.ID
          ,B.*
     From  @YourTable A
     Cross Apply (
                    Select RetSeq = Row_Number() over (Order By (Select null))
                          ,RetVal = LTrim(RTrim(B.i.value('(./text())[1]', 'varchar(max)')))
                    From  (Select x = Cast('<x>' + replace((Select replace(replace(A.SomeText,char(13),' '),' ','§§Split§§') as [*] For XML Path('')),'§§Split§§','</x><x>')+'</x>' as xml).query('.')) as A 
                    Cross Apply x.nodes('x') AS B(i)
                 ) B
     Where B.RetVal like '#%'
    

    退货

    ID  RetSeq  RetVal
    1   1       #want
    1   5       #hastag
    1   10      #delhi
    1   11      #Traffic
    2   1       #bunny
    2   2       #hastag
    2   3       #donetodeath
    

    【讨论】:

    • 谢谢!这非常有帮助,但是我发现当字符串中有新行时它不会提取主题标签。例如,如果字符串中的新行后面有主题标签,则不会返回这些标签,并且由于我不熟悉 SQL 中的字符串操作方法,我不知道如何完成该操作
    • @teku45 在此处查看 UDF stackoverflow.com/questions/42958278/… 或者您可以添加 ... replace(A.SomeText,char(13),' ')
    猜你喜欢
    • 1970-01-01
    • 2018-07-15
    • 2017-04-18
    • 2017-04-12
    • 2016-06-20
    • 2018-02-03
    • 2010-12-09
    • 1970-01-01
    • 2015-01-15
    相关资源
    最近更新 更多