Select field from table where a field like Condition

Source: Internet
Author: User

Select field from table where a field like Condition
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, if select * from [user] Where u_name like '% 3%', u_name is set to "Zhang San", "Zhang Mao San", and "three-legged cats ", "Tang sanzang" and so on have all records of "3. 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 you use select * from [user] Where u_name like '% 3% cat %', although you can search for three-legged cats, you cannot find the three-legged cats that meet the search criteria ".


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 finds "Tang sanzang". In this way, u_name is three characters and the middle word is "three; for example, select * from [user] Where u_name like 'three _ '; only the three-legged cat is found. The name 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] san' will find "Zhang San", "Li San", and "Wang San" (rather than "Zhang Li Wang San "); for example, [] contains a series of characters (01234, ABCDE, and so on) it can be written as "0-4", "a-e" select * from [user] Where u_name like 'Old [1-9] ', will find "Old 1", "old 2 ",...... And "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] san' will find "Zhao San" without the surname "Zhang", "Li", and "Wang ", sun San and others; select * from [user] Where u_name like 'Old [^ 1-4] '; will exclude "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 the wildcard, when special characters are included in "[]", they can be queried normally. Based on this, we write the following function: function sqlencode (STR) STR = Replace (STR, "[", "[]") ', which must be at the beginning of STR = Replace (STR, "_", "[_]") STR = Replace (STR, "%", "[%]") sqlencode = STR end function process the string to be queried before query.

This article is from "playing Linux... O & M... Cluster blog, be sure to keep this source http://scottlinn.blog.51cto.com/829656/1567797

Select field from table where a field like Condition

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.