SELECT field from table WHERE a field like condition

Source: Internet
Author: User
SELECT field from table WHERE a field like condition
With regard to conditions, SQL provides four matching modes:

1,%: Represents any 0 or more characters.  Can match any type and length of characters, in some cases in Chinese, please use two percent sign (%).  For example, SELECT * from [user] WHERE u_name like '% Three ', will be u_name as "John", "Zhang Cat Three", "three-legged Cat", "Tang Sanzang" and so on Have "three" records all find out. In addition, if you need to find a record in u_name that has both "three" and "cat", use the AND condition, select * FROM [user] WHERE u_name like '% three ' and u_name like '% cat ', if you use SELECT * From [user] WHERE u_name is like '% of cat% ' although it can search out "three-legged cat", but can not search out the conditions of the "cat three".


2,_: Represents any single character. Matches a single arbitrary character, which is used to restrict the character-length statements of an expression: for example, select * from [user] WHERE u_name like ' _ three _ ' Only find "Tang Sanzang" so U_name is three words and the middle one is "three"; From [user] WHERE u_name like ' three __ '; Only find "three-legged cat" so name is three words and the first word is "three".


3,[]: Represents one of the characters listed in parentheses (similar to a regular expression).  Specifies a character, string, or range, which requires that the matched objects be any of them. For example, SELECT * from [user] WHERE u_name like ' [Zhang Li Wang] III ' will find "John", "Lie Triple Systems", "Wang San" (not "Zhangli Kang"), such as a series of characters in [] (01234, ABCDE, etc.) can be slightly written as "0-4", " 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 that is not listed within the parentheses.  The value and [] are the same, 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] III ' will find out not the surname "Zhang", "Li", "Wang" "Zhao Three", "Sun San", etc. select * from [user] where u_name like ' old [^1-4] ' will exclude "old 1" to "Old 4", Looking for "old 5", "Old 6" 、......


5, the query contains wildcard characters, because of the wildcard character, cause we query special characters "%", "_", "[" statement can not be implemented normally, and special characters with "[]" around the normal query. Accordingly we write the following functions: function Sqlencode (str) str=replace (str, "[", "[[]") ' This sentence must be in the front str=replace (str, "_", "[_]") Str=replace (str, "%", "[%]") The Sqlencode=str End Function processes the search string before the query.

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.