Detailed SELECT LIKE usage in SQL query statements

  
In the SQL structured query language, the LIKE statement plays a crucial role.

LIKE statement syntax is:
select * from table where field names like the corresponding value (substring),
it mainly for its role in a character field is character The type field column retrieves the corresponding substring.

A:% any string of zero or more characters:

1, LIKE'Mc% 'search begins with the letters Mc all strings (e.g. McBadden).

2, LIKE '% inger' searches for all strings that ends with inger (e.g. Ringer, Stringer).

3, LIKE '% en%' searches for all strings that contain the letters en anywhere (e.g., Bennet, Green, McBadden).

B: _ (underscore) Any single character:

LIKE'_heryl 'will search for all six-letter name ends with the letter heryl (such as Cheryl, Sheryl).

C:
[] specified range ([AF]) or set ([abcdef]) in any single character:

1,

LIKE '[ ,null,null,0],CK]ars[eo]n' will search for the following strings:
Carsen, Karsen, Carson, and Karson (like Carson).

2, LIKE '[M-Z] inger' searches for all names ending string inger, any single letter M to Z from the beginning (e.g., Ringer).

D:
[^] does not belong to the specified range ([AF]) or set ([abcdef]) any single character:

LIKE'M [^ c]% ' Searches begin with the letter M,
and the second letter is not all names of c (such as MacFeather).

E:
* It is the same command in DOS wildcard,
represent multiple characters:

c * c representatives cc, cBc, cbc, cabdfec and other characters .

F:
? Same as in the DOS command? Wildcard,
represent a single character:? B b representatives brb, bFb and other

G:
# roughly above,
different generations can only represent a single digit.
k#k stands for k1k, k8k, k0k.

F:
exclude it represents a single character

Let's take an example to illustrate [!]:

Example 1,
query name field contains There is a "clear" word.

select * from table1 where name like '% out%'

Example 2,
query name field to & ldquo; Li & rdquo; leading word.

select * from table1 where name like 'Li *'

Example 3,
query name field containing digits.

select * from table1 where name like '% [0-9]%'

Example 4,
query name field with lowercase letters.

select * from table1 where name like '% [a-z]%'

Example 5,
query field name does not contain numbers.

select * from table1 where name like '% [! 0-9]%'

above examples can list what value is obvious.
But here, we are important to note the difference between the wildcard “*” and “%”.

many of my friends will ask,
why I have individual representation when all the characters used in the above query "% " instead of & ldquo; * & rdquo ;? Look at the following example of what can occur are the result of:

select * from table1 where name like '* next *'

select * from table1 where name like '% next%'

You will see that
the previous statement lists all the records and the latter record lists the records that contain the "name" in the name field,
so when we make characters When the type field contains a substring query, it is better to use “%” instead of “*”,
use“*” only at the beginning or only at the end but not at both ends<quo ;*”In the case of replacing any character.
Copyright © Windows knowledge All Rights Reserved