SQL statements used to determine whether a string contains a string

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.