SQL 服务器中的全词搜索
Full word search in SQL Server
你能告诉我如何在 SQL 服务器中进行全词通配符搜索吗?
假设我有以下 table
CREATE TABLE Table1
(
ID number,
Text varchar(3000)
)
这是我的数据:
ID
Text
Note
1
THIS IS MY CAR
(*Note: "CAR" is the last 3 character of the string)
2
MY DADDY LOOK AFTER ME CAREFULLY
(Note: This sentence does not have the word "CAR")
3
THIS IS MY CAR:). I LOOK AFTER IT CAREFULLY EVERYDAY.
(Note: there is a full stop after the word "CAR")
4
CAR- IS MY LIFE. I LOVE IT MORE THAN EVERYTHING
(Note: There is special character after the word "CAR")
5
CAR!
6
CAR
7
TOO MUCH CARBON DIOXIDE IS BAD FOR HEALTH!
(Note: This sentence does not have the word "CAR")
8
LUCKLY THAT THIS TWINS DOES NOT HAVE ACARDIA.
现在,我需要编写一个查询来列出包含单词“CAR”的所有行。
(即以下是我想要的结果)
ID
Text
1
THIS IS MY CAR
3
THIS IS MY CAR:). I LOOK AFTER IT CAREFULLY EVERYDAY.
4
CAR- IS MY LIFE. I LOVE IT MORE THAN EVERYTHING
5
CAR!
6
CAR
其中 ID 2,7 和 8 被省略,因为它们没有单词“CAR”,即使它们有一些包含三个字符“C-A-R”的单词。
任何人都可以与我分享一些关于在 SQL 服务器中使用 SQL 的知识。
提前致谢!
执行此操作的一种方法(不会很快)如下:
DECLARE @string varchar(20) = 'CAR';
SELECT *
FROM dbo.YourTable
WHERE CONCAT('#',TextColumn,'#') LIKE CONCAT('%[^a-z]',@string,'[^a-z]%')
;
我很抱歉。我被问题弄糊涂了,我调整了答案,见下文
select * from Table_10
where ('.' + Table_10.Text + '.') LIKE '%[^a-z]CAR[^a-z]%'
您可以使用包含来搜索任何字词或短语:
select * from table1 where Contains(text,'CAR');
你能告诉我如何在 SQL 服务器中进行全词通配符搜索吗?
假设我有以下 table
CREATE TABLE Table1
(
ID number,
Text varchar(3000)
)
这是我的数据:
ID | Text | Note |
---|---|---|
1 | THIS IS MY CAR | (*Note: "CAR" is the last 3 character of the string) |
2 | MY DADDY LOOK AFTER ME CAREFULLY | (Note: This sentence does not have the word "CAR") |
3 | THIS IS MY CAR:). I LOOK AFTER IT CAREFULLY EVERYDAY. | (Note: there is a full stop after the word "CAR") |
4 | CAR- IS MY LIFE. I LOVE IT MORE THAN EVERYTHING | (Note: There is special character after the word "CAR") |
5 | CAR! | |
6 | CAR | |
7 | TOO MUCH CARBON DIOXIDE IS BAD FOR HEALTH! | (Note: This sentence does not have the word "CAR") |
8 | LUCKLY THAT THIS TWINS DOES NOT HAVE ACARDIA. |
现在,我需要编写一个查询来列出包含单词“CAR”的所有行。
(即以下是我想要的结果)
ID | Text |
---|---|
1 | THIS IS MY CAR |
3 | THIS IS MY CAR:). I LOOK AFTER IT CAREFULLY EVERYDAY. |
4 | CAR- IS MY LIFE. I LOVE IT MORE THAN EVERYTHING |
5 | CAR! |
6 | CAR |
其中 ID 2,7 和 8 被省略,因为它们没有单词“CAR”,即使它们有一些包含三个字符“C-A-R”的单词。
任何人都可以与我分享一些关于在 SQL 服务器中使用 SQL 的知识。
提前致谢!
执行此操作的一种方法(不会很快)如下:
DECLARE @string varchar(20) = 'CAR';
SELECT *
FROM dbo.YourTable
WHERE CONCAT('#',TextColumn,'#') LIKE CONCAT('%[^a-z]',@string,'[^a-z]%')
;
我很抱歉。我被问题弄糊涂了,我调整了答案,见下文
select * from Table_10
where ('.' + Table_10.Text + '.') LIKE '%[^a-z]CAR[^a-z]%'
您可以使用包含来搜索任何字词或短语:
select * from table1 where Contains(text,'CAR');