~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to docs/functions/string/comparative.rst

edit

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
Comparative Functions
2
 
=====================
3
 
 
4
 
LIKE            
5
 
----
6
 
 
7
 
The LIKE operator is used to check if field values match a specified pattern, and searches for less-than-exact but similar values.
8
 
 
9
 
An initial example is: ::
10
 
 
11
 
        SELECT 'ä' LIKE 'ae' COLLATE latin1_german2_ci;
12
 
 
13
 
Returns 0
14
 
 
15
 
Whereas ::
16
 
 
17
 
        SELECT 'ä' = 'ae' COLLATE latin1_german2_ci;
18
 
 
19
 
Returns 1
20
 
 
21
 
The LIKE operator supports the use of two wildcards. (Wildcards provide more flexibility by allowing any character or group of characters in a string to be acceptable as a match for another string):
22
 
 
23
 
    * Percentage (%): Represents zero or more values.
24
 
    * Underscore (_): Matches exactly one character value.
25
 
 
26
 
In accordance the SQL standard, LIKE performs matching on a per-character basis. It therefore provides results different from the = comparison operator.
27
 
 
28
 
The following SELECT statement includes a WHERE clause in order to search for job titles that start with "DIRECTOR", by using the percentage wildcard after the lookup value.
29
 
 
30
 
For example: ::
31
 
 
32
 
        SELECT title, field
33
 
        FROM job_detail
34
 
        WHERE title LIKE 'DIRECTOR%'
35
 
        ORDER BY field, title;
36
 
 
37
 
 
38
 
REGEXP
39
 
------
40
 
 
41
 
Returns values that match a regular expression pattern; they are commonly used for creating complex searches. Here is an example of using a REGEXP (Regular Expression) match: ::
42
 
 
43
 
        SELECT title, category_name
44
 
        FROM film_detail
45
 
        WHERE title REGEXP '^AIRP[LO]'
46
 
        ORDER BY title;
47
 
 
48
 
Other REGEXP examples: :;
49
 
 
50
 
        SELECT 'abcabc' REGEXP 'abc',    
51
 
        'abcabc' REGEXP 'cb';
52
 
 
53
 
The search pattern may describe only a part of string. To match entire string, use ^ and $ in the search: ::
54
 
 
55
 
        SELECT 'abc' REGEXP '^abc$', 'abcabc' REGEXP '^abc$';
56
 
 
57
 
 
58
 
        SELECT 'cde' REGEXP '[a-c]+', 'efg' REGEXP '[a-c]+';
59
 
 
60
 
 
61
 
        SELECT 'abcabc' REGEXP 'ABC', 'abcabc' REGEXP BINARY 'ABC';
62
 
 
63
 
 
64
 
STRCMP()
65
 
--------
66
 
 
67
 
The purpose of STRCMP is also to compare two strings. This function returns 0 if two strings are the same, -1 if the first argument is smaller than the second according to the current sort order, and 1 otherwise.