我理解这里的答案:How to extract hashtags from a string in T-SQL 解释了如何从声明的字符串变量中提取主题标签,但是如何将此操作应用于整个字符串列呢?
答案 0 :(得分: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