【发布时间】:2017-05-31 17:32:22
【问题描述】:
我明白这个答案在这里:How to extract hashtags from a string in T-SQL 解释了如何从声明的字符串变量中提取主题标签,但是如何将此操作应用于整个字符串列?
【问题讨论】:
标签: sql-server tsql
我明白这个答案在这里:How to extract hashtags from a string in T-SQL 解释了如何从声明的字符串变量中提取主题标签,但是如何将此操作应用于整个字符串列?
【问题讨论】:
标签: sql-server tsql
使用交叉应用。 只是为了好玩,删除最后的 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
【讨论】: