ColdFusion lets you use the following conditional operators in your SQL statements:
This conditional tests whether a Boolean expression is True, False, or Unknown.
cond_test ::= expression [IS [NOT] {TRUE | FALSE | UNKNOWN} ]
SELECT _isValid FROM Chemicals WHERE _isValid IS true;
This conditional tests whether an expression is null.
null_cond ::= expression IS [NOT] NULL
SELECT bloodVal FROM Standards WHERE bloodVal IS NOT null;
This conditional lets you compare an expression against another expression of the same data type (Numeric, String, Date, or Boolean). You can use it to selectively retrieve only the relevant rows of a record set.
comparison_cond ::= expression [> | >= | <> | != | < | <=] expression
The following example uses a comparison conditional to retrieve only those dogs whose IQ is at least 150:
SELECT dog_name, dog_IQ FROM Dogs WHERE dog_IQ >= 150;
This conditional lets you compare an expression against another expression. You can use it to selectively retrieve only the relevant rows of a record set. Like the comparison conditional, the BETWEEN conditional makes a comparison; however, the between conditional makes a comparison against a range of values. Therefore, its syntax requires two values, which are inclusive, a minimum and a maximum. You must separate these values with the AND keyword.
between_cond ::= expression [NOT] BETWEEN expression AND expression
The following example uses a BETWEEN conditional to retrieve only those dogs whose IQ is between 150 and 165, inclusive:
SELECT dog_name, dog_IQ FROM Dogs WHERE dog_IQ BETWEEN 150 AND 165;
This conditional lets you specify a comma-delimited list of conditions to match. It is similar in function to the OR conditional. In addition to being more legible when working with long lists, the IN conditional can contain another SELECT statement.
in_cond ::= expression [NOT] IN (expression_list)
The following example uses the IN conditional to retrieve only those dogs who were born at either Ken's Kennels or Barb's Breeders:
SELECT dog_name, dog_IQ, Kennel_ID FROM Dogs WHERE kennel_ID IN ('Kens','Barbs');
This conditional lets you perform wildcard searches, in which you compare your data to search patterns. This strategy differs from other conditionals, such as BETWEEN or IN, because the LIKE conditional compares your data to a value that is partially unknown.
like_cond ::= left_string_exp [NOT] LIKE right_string_exp [ESCAPE escape_char]
The left_string_exp can be either a constant string, or a column reference to a string column. The right_string_exp can be either a column reference to a string column, or a search pattern. A search pattern is a search condition that consists of literal text and at least one wildcard character. A wildcard character is a special character that represents an unknown part of a search pattern, and is interpreted as follows:
Note: Earlier versions of ColdFusion do not support bracketed ranges.
The following example uses the LIKE conditional to retrieve only those dogs of the breed Terrier, whether the dog is a Boston Terrier, Jack Russell Terrier, Scottish Terrier, and so on:
SELECT dog_name, dog_IQ, breed FROM Dogs WHERE breed LIKE '%Terrier';
The following examples are select statements that use bracketed ranges:
SELECT lname FROM Suspects WHERE lname LIKE 'A[^c]%'; SELECT lname FROM Suspects WHERE lname LIKE '[a-m]%'; SELECT lname FROM Suspects WHERE lname LIKE '%[]'; SELECT lname FROM Suspects WHERE lname LIKE 'A[%]%'; SELECT lname FROM Suspects WHERE lname LIKE 'A[^c-f]%';
Unlike the rest of ColdFusion, Query of Queries is case-sensitive. However, Query of Queries supports two string functions, UPPER()
and LOWER()
, which you can use to achieve case-insensitive matching.
The following example matches only 'Sylvester':
SELECT dog_name FROM Dogs WHERE dog_name LIKE 'Sylvester';
The following example is not case-sensitive; it uses the LOWER()
function to treat 'Sylvester', 'sylvester', 'SYLVESTER', and so on as all lowercase, and matches them with the all lowercase string, 'sylvester':
SELECT dog_name FROM Dogs WHERE LOWER(dog_name) LIKE 'sylvester';
If you use a variable on the right side of the LIKE conditional and want to ensure that the comparison is not case-sensitive, use the LCase
or UCase
function to force the variable text to be all of one case, as in the following example:
WHERE LOWER(dog_name) LIKE '#LCase(FORM.SearchString)#';
You can specify your own escape character using the conditional ESCAPE clause.
The following example uses the ESCAPE clause to enable a search for a literal percent sign (%), which ColdFusion normally interprets as a wildcard character:
SELECT emp_discount FROM Benefits WHERE emp_discount LIKE '10\%' ESCAPE '\';