A detailed explanation of SQL Fuzzy query statement

Source: Internet
Author: User

To implement a fuzzy query in SQL We just need to use like to be able to achieve, with some of the middle of the parameters such as%? And so on, let's look at an example.
Like operator
The LIKE operator is used to search for a specified pattern in a column in a WHERE clause.

SQL Like operator syntax

SELECT column_name (s) from Table_namewhere column_name like

1> CREATE TABLE Employee (emp_no INTEGER not NULL,
2> emp_fname CHAR (not NULL),
3> emp_lname CHAR (not NULL),
4> dept_no CHAR (4) NULL)
5> Go
1> INSERT into employee values (1, ' Matthew ', ' Smith ', ' D3 ')
2> INSERT into employee values (2, ' Ann ', ' Jones ', ' D3 ')
3> INSERT into employee values (3, ' John ', ' barrimore ', ' D1 ')
4> INSERT into employee values (4, ' James ', ' James ', ' D2 ')
5> INSERT into employee values (5, ' Elsa ', ' Bertoni ', ' D2 ')
6> INSERT into employee values (6, ' Elke ', ' Hansel ', ' D2 ')
7> INSERT into employee values (7, ' Sybill ', ' Moser ', ' D1 ')
8> Go


> SELECT * FROM Employee WHERE emp_fname isn't like '%n '
4> Go
Emp_no emp_fname Emp_lname Dept_no
----------- -------------------- -------------------- -------
1 Matthew Smith D3
4 James James D2
5 Elsa Bertoni D2
6 Elke Hansel D2
7 Sybill Moser D1

Example Two

2> SELECT *
3> from Employee
4> WHERE Name like "%[k-l]%"
5> Go
ID Name Salary start_date City Region
----------- ---------- ----------- ----------------------- ---------- ------
3 Celia 24020 1996-12-03 00:00:00.000 Toronto W
4 Linda 40620 1997-11-04 00:00:00.000 New York N
7 Alison 90620 2000-08-07 00:00:00.000 New York W

About like%

#% represents any number of characters

SELECT * from user where username like '%huxiao ';

SELECT * from user where username like ' huxiao% ';

SELECT * from user where username like '%huxiao% ';

#% represents a character

SELECT * from user where username like ' _ ';

SELECT * from user where username like ' ___ ';

SELECT * from user where username like ' Huxia_ ';

SELECT * from user where username like ' H_xiao ';


If I really want to check% or _, how to do? Using escape, the% or _ after the escape character is not used as a wildcard, noting that the% and _ of the previous escape characters are still in the wildcard role

Select username from Gg_user where username like '%xiao/_% ' escape '/';
Select username from Gg_user where username like '%xiao/%% ' escape '/';

About wildcard characters

' A_z ': All characters that start with ' A ', another word of any value, and a string ending with ' Z '. Both ' Abz ' and ' a2z ' conform to this pattern, and ' akkz ' does not conform (because there are two characters between A and Z, not a single word).
' abc% ': all the strings starting with ' ABC '. For example, ' ABCD ' and ' abcabc ' all fit this set.
'%XYZ ': all strings ending with ' XYZ '. For example, ' wxyz ' and ' zzxyz ' are consistent with this set.
'%an% ': All containing ' an ' this set of strings. For example, both ' LOS ANGELES ' and ' SAN FRANCISCO ' fit this set.

In SQL, you can use the following wildcard characters:

wildcard characters Description
% Replace one or more characters
_ Replace only one character
[Charlist] Word columns any single character

[^charlist]

Or

[!charlist]

Not a word columns any single

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.