按 sql 提取主题标签
Extracting hashtags by sql
我有这个问题 - 目前我正在 C# 应用程序中提取主题标签,但现在我想将它移动到 SQL 服务器。
在 C# 中,我有使用正则表达式的代码:
var regex = new Regex(@"#\w+");
var matches = regex.Matches(item.Contents).ToList();
在T-SQL,我想要这样的东西:
--I have table #Keywords like this:
CREATE TABLE #Keywords
(
Word nvarchar(400),
Id int
)
INSERT INTO #Keywords VALUES ('This is #text1 with #hashtag1', 1);
INSERT INTO #Keywords VALUES ('This is #text2 with #hashtag2', 2);
INSERT INTO #Keywords VALUES ('This is #text3 with #hashtag3', 3);
SELECT * FROM #Keywords
-- In result I want table like this:
CREATE TABLE #HashtagsResult
(
Word nvarchar(400),
Id int
)
INSERT INTO #HashtagsResult VALUES ('#text1', 1);
INSERT INTO #HashtagsResult VALUES ('#hashtag1', 1);
INSERT INTO #HashtagsResult VALUES ('#text2', 2);
INSERT INTO #HashtagsResult VALUES ('#hashtag2', 2);
INSERT INTO #HashtagsResult VALUES ('#text3', 3);
INSERT INTO #HashtagsResult VALUES ('#hashtag3', 3);
SELECT * FROM #HashtagsResult
使用 string_split
SELECT id, s.value
FROM #Keywords
cross apply string_split(Word, ' ') s
where s.value like '#%'
我有这个问题 - 目前我正在 C# 应用程序中提取主题标签,但现在我想将它移动到 SQL 服务器。
在 C# 中,我有使用正则表达式的代码:
var regex = new Regex(@"#\w+");
var matches = regex.Matches(item.Contents).ToList();
在T-SQL,我想要这样的东西:
--I have table #Keywords like this:
CREATE TABLE #Keywords
(
Word nvarchar(400),
Id int
)
INSERT INTO #Keywords VALUES ('This is #text1 with #hashtag1', 1);
INSERT INTO #Keywords VALUES ('This is #text2 with #hashtag2', 2);
INSERT INTO #Keywords VALUES ('This is #text3 with #hashtag3', 3);
SELECT * FROM #Keywords
-- In result I want table like this:
CREATE TABLE #HashtagsResult
(
Word nvarchar(400),
Id int
)
INSERT INTO #HashtagsResult VALUES ('#text1', 1);
INSERT INTO #HashtagsResult VALUES ('#hashtag1', 1);
INSERT INTO #HashtagsResult VALUES ('#text2', 2);
INSERT INTO #HashtagsResult VALUES ('#hashtag2', 2);
INSERT INTO #HashtagsResult VALUES ('#text3', 3);
INSERT INTO #HashtagsResult VALUES ('#hashtag3', 3);
SELECT * FROM #HashtagsResult
使用 string_split
SELECT id, s.value
FROM #Keywords
cross apply string_split(Word, ' ') s
where s.value like '#%'