wildcard characters in SQL Server

Source: Internet
Author: User

There are currently three types of SQL Server wildcard characters, "%", "_", "[charlist]", and three are used for fuzzy queries.

The following are presented separately:

1, "%"

"%" is used for any string containing 0 or more characters, instead of the indeterminate part of the query condition, in general, the% wildcard is used to match the following characters, and is a full-text search with a slower speed

Example: SELECT * from PRODUCEDATATB WHERE serialnum like '%2317371611290859 ' This is the query statement that I used to query for other characters in the enterprise production database before the serial number, Execution time is 1250ms

2, "_"

"_" to match any single character

Change the above example to SELECT * from PRODUCEDATATB WHERE serialnum like ' __2317371611290859 ', at which point only the first two bits of the serial number match, the speed is 3 times times higher than the use of "%", Execution time is 420ms

3. "[Charlist]"

Select the data that is included in [] with the beginning of the letter

Example:


The "Persons" table selects people who live in cities that start with "A" or "L" or "N"


The "Persons" table selects people who live in cities that do not start with "A" or "L" or "N"


How to view the execution time of an SQL statement:
A simpler and more straightforward approach:

DECLARE @begin_date datetime
DECLARE @end_date datetime
Select @begin_date = getdate ()

SELECT * from PRODUCEDATATB WHERE serialnum like ' __2317371611290859 ' // Execute SQL statement
--select * from PRODUCEDATATB WHERE serialnum= ' D02317371611290859 '
--select * from PRODUCEDATATB WHERE serialnum like '%2317371611290859 '

Select @end_date = getdate ()
Select DateDiff (MS, @begin_date, @end_date) as ' spents/milliseconds '

wildcard characters in SQL Server

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.