How can I query something that contains a keyword from SQL Server?
Generally, there are two methods:
1. Use like --
The code is as follows: |
Copy code |
Select * from tablename where field1 like '% key %'
|
This method can be used to check the words in a sentence.
2. Use charindex () -- charindex (character, string)> 0-> to include
This can be used to view the words in a paragraph or article. ^_^
In the SQL program, in addition to the above method, we can also use functions to operate
Assume that the data in the Article table is as follows:
The code is as follows: |
Copy code |
Select ID, title, author from Article |
To determine whether a given string contains the value in the title field, use the following method:
1. Use the replace () function
The code is as follows: |
Copy code |
Declare @ item nvarchar (100) Set @ item = 'difficult English '; Select ID, title, author from Article Where LEN (REPLACE (@ item, title, '') <len (@ item); -- judge based on the length after replacement> 2, |
Use the charindex () function
The code is as follows: |
Copy code |
Select ID, title, author from Article where CHARINDEX (title, @ item)> 0 |
This achieves our goal.