我想使用 SQL 服务器查询过滤字符串中所有出现的模式。例如:“#Hello world! Need to #filter the #tag” 在这个字符串中需要过滤主题标签词...结果应该是... “#Hello” “#filter” “#tag”
4 回答
0
请确认这是否适合您:
declare @tempTable table (hashtag varchar(Max))
declare @asdf varchar(100) = '#Hello world! Need to #filter the #tag'
declare @i integer =0
declare @length integer = (select len(@asdf)-len(replace(@asdf,'#','')))
while (select @i)< @length
begin
insert into @tempTable select substring(@asdf,charindex('#', @asdf),charindex(' ', @asdf))
set @asdf = ltrim((select substring(substring(@asdf,charindex('#', @asdf),len(@asdf)),charindex(' ', @asdf)+1,len(@asdf))))
set @i = @i+1
end
select HashTag from @tempTable
于 2019-03-18T15:12:30.207 回答
0
根据问题上显示的信息,您可能指的是这样的内容:
SELECT * FROM table_A WHERE column_A LIKE '#%'
此查询将为您提供表 table_A 中 column_A 列中的所有字段,并带有标签符号
于 2019-03-18T13:44:16.690 回答
0
使用字符串拆分器非常简单。我正在使用 Jeff Moden 的分离器,您可以在此处找到。
declare @asdf varchar(100) = '#Hello world! Need to #filter the #tag'
select *
from dbo.DelimitedSplit8K(@asdf, ' ')
where Item like '#%'
于 2019-03-18T13:45:06.793 回答
0
如果 Sean Lange 是正确的并且应该避免循环,那么这是另一个无需循环即可工作的答案:
declare @asdf varchar(100) = '#Hello world! Need to #filter the #tag'
SELECT
LTRIM(RTRIM(m.n.value('.[1]','varchar(8000)'))) AS HashTag
FROM
(
SELECT CAST('<Records><Row>' + REPLACE(@asdf,' ','</Row><Row>') + '</Row></Records>' AS XML) AS x
)t
CROSS APPLY x.nodes('/Records/Row')m(n)
where LTRIM(RTRIM(m.n.value('.[1]','varchar(8000)'))) like '#%'
然后,您可以将上述逻辑合并到可以应用于每一行的标准 SELECT 语句中。
于 2019-03-18T15:50:06.843 回答