In the WHERE clause, you can use the like clause with wildcards for columns of the datetime, Char, and varchar field types to select data records that are similar to.... The following are available wildcards:
% Zero or multiple characters
_ Any single character (underline)
/Special characters
[] Characters in a certain range, such as [0-9] or [aeth]
[^] Characters out of a certain range, such as [^ 0-9] or [^ aeth]
SQL provides four matching modes for conditions:
1, %: represents any 0 or multiple characters. It can match any type and length of characters. In some cases, if it is Chinese, use two percent signs (%.
For example, select * from [user] Where u_name like '% 3%'
We will find all records with "3", such as "3 Zhang", "3 Zhang", "3 Zhang Mao", and "3 Tang sanzang.
In addition, if you need to find records with "three" and "cat" in u_name, use the and condition.
Select * from [user] Where u_name like '% 3%' and u_name like '% cat %'
If select * from [user] Where u_name like '% 3% cat %' is used'
Although three-legged cats can be searched, three-legged cats cannot be searched ".
2, _: represents any single character. Matches any character. It is often used to limit the character length of an expression:
For example, select * from [user] Where u_name like '_ 3 _'
Only find that the u_name such as "Tang sanzang" is three characters and the middle word is "three;
For example, select * from [user] Where u_name like 'three __';
Find out that the name of the "three-legged cat" is three characters and the first word is "three;
3, []: represents one of the characters listed in brackets (similar to a regular expression ). Specifies a character, string, or range. The matching object must be one of them.
For example, select * from [user] Where u_name like '[Zhang Li Wang] 3'
We will find "Zhang San", "Li San", and "Wang San" (rather than "Zhang Li Wang San ");
For example, if [] contains a series of characters (01234, ABCDE, etc.), it can be slightly written as "0-4" or "a-e"
Select * from [user] Where u_name like 'Old [1-9]'
Will find "Old 1", "old 2 ",...... , "Old 9 ";
4, [^]: represents a single character not listed in parentheses. The value is the same as [], but it requires that the matched object be any character other than the specified character.
For example, select * from [user] Where u_name like '[^ Zhang Li Wang] 3'
We will find "Zhao San" and "Sun San" without the surname "Zhang", "Li", and "Wang;
Select * from [user] Where u_name like 'Old [^ 1-4] ';
From "Old 1" to "old 4", search for "old 5", "old 6 ",......
5. When the query content contains wildcards
The special characters "%", "_", and "[" cannot be properly queried due to wildcards, when special characters are included in "[]", they can be queried normally. Then, write the following functions:
Function sqlencode (STR)
STR = Replace (STR, "[", "[]") 'must be at the beginning
STR = Replace (STR, "_", "[_]")
STR = Replace (STR, "%", "[%]")
Sqlencode = Str
End Function
The string to be queried can be processed by this function before query.