SQL Server alternative notation for like statements

Source: Internet
Author: User

It is well known to mention like statements, for example, to find all users with "C" in their username, we can use the MyDatabase select * FROM table1 where username '%c% '

Here's another way to write the above function:use mydatabase
select * from table1 where charindex('c',username)>0

This method in theory than the previous method more than a judgment statement, that is, >0, but this process is the fastest, I believe that more than 80% of the operations are spent looking for strings and other operations, so the use of charindex function is not a big deal. This method also has the advantage, that is to%,| and so on can not be directly with like to find characters in the use of this charindex directly, as follows:

use mydatabase
select * from table1 where charindex('%',username)>0

You can also write:

use mydatabase
select * from table1 where charindex(char(37),username)>0

The ASCII character is%

Related Article

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.