Search Condition

Transact-SQL Reference

Transact-SQL Reference

Search Condition

Is a combination of one or more predicates using the logical operators AND, OR, and NOT.

Syntax

< search_condition > ::=
    {    [ NOT ] < predicate > | ( < search_condition > ) }
        [ { AND | OR } [ NOT ] { < predicate > | ( < search_condition > ) } ]
    }    [ ,...n ]

< predicate > ::=
    
{    expression { = | < > | ! = | > | > = | ! > | < | < = | ! < } expression
        | string_expression [ NOT ] LIKE string_expression
            [ ESCAPE 'escape_character' ]
        | expression [ NOT ] BETWEEN expression AND expression
        | expression IS [ NOT ] NULL
        | CONTAINS
            ( { column | * } , '< contains_search_condition >' )
        | FREETEXT ( { column | * } , 'freetext_string' )
        | expression [ NOT ] IN ( subquery | expression [ ,...n ] )
        | expression { = | < > | ! = | > | > = | ! > | < | < = | ! < }
            { ALL | SOME | ANY} ( subquery )
        | EXISTS ( subquery )
    
}

Arguments

< search_condition >

Specifies the conditions for the rows returned in the result set for a SELECT statement, query expression, or subquery. For an UPDATE statement, specifies the rows to be updated. For a DELETE statement, specifies the rows to be deleted. There is no limit to the number of predicates that can be included in a Transact-SQL statement search condition.

NOT
Negates the Boolean expression specified by the predicate. For more information, see NOT.
AND
Combines two conditions and evaluates to TRUE when both of the conditions are TRUE. For more information, see AND.
OR
Combines two conditions and evaluates to TRUE when either condition is TRUE. For more information, see OR.

< predicate >

Is an expression that returns TRUE, FALSE, or UNKNOWN.

expression
Is a column name, a constant, a function, a variable, a scalar subquery, or any combination of column names, constants, and functions connected by an operator(s) or a subquery. The expression can also contain the CASE function.
=
Is the operator used to test the equality between two expressions.
<>
Is the operator used to test the condition of two expressions not being equal to each other.
!=
Is the operator used to test the condition of two expressions not being equal to each other.
>
Is the operator used to test the condition of one expression being greater than the other.
>=
Is the operator used to test the condition of one expression being greater than or equal to the other expression.
!>
Is the operator used to test the condition of one expression not being greater than the other expression.
<
Is the operator used to test the condition of one expression being less than the other.
<=
Is the operator used to test the condition of one expression being less than or equal to the other expression.
!<
Is the operator used to test the condition of one expression not being less than the other expression.
string_expression
Is a string of characters and wildcard characters.
[ NOT ] LIKE
Indicates that the subsequent character string is to be used with pattern matching. For more information, see LIKE.
ESCAPE 'escape_character'
Allows a wildcard character to be searched for in a character string instead of functioning as a wildcard. escape_character is the character that is placed in front of the wildcard character to denote this special use.
[ NOT ] BETWEEN
Specifies an inclusive range of values. Use AND to separate the beginning and ending values. For more information, see BETWEEN.
IS [ NOT ] NULL
Specifies a search for null values, or for values that are not null, depending on the keywords used. An expression with a bitwise or arithmetic operator evaluates to NULL if any of the operands is NULL.
CONTAINS
Searches columns containing character-based data for precise or "fuzzy" (less precise) matches to single words and phrases, the proximity of words within a certain distance of one another, and weighted matches. Can only be used with SELECT statements. For more information, see CONTAINS.
FREETEXT
Provides a simple form of natural language query by searching columns containing character-based data for values that match the meaning rather than the exact words in the predicate. Can only be used with SELECT statements. For more information, see FREETEXT.
[ NOT ] IN
Specifies the search for an expression, based on the expression's inclusion in or exclusion from a list. The search expression can be a constant or a column name, and the list can be a set of constants or, more commonly, a subquery. Enclose the list of values in parentheses. For more information, see IN.
subquery
Can be considered a restricted SELECT statement and is similar to <query_expresssion> in the SELECT statement. The ORDER BY clause, the COMPUTE clause, and the INTO keyword are not allowed. For more information, see SELECT.
ALL
Used with a comparison operator and a subquery. Returns TRUE for <predicate> if all values retrieved for the subquery satisfy the comparison operation, or FALSE if not all values satisfy the comparison or if the subquery returns no rows to the outer statement. For more information, see ALL.
{ SOME | ANY }
Used with a comparison operator and a subquery. Returns TRUE for <predicate> if any value retrieved for the subquery satisfies the comparison operation, or FALSE if no values in the subquery satisfy the comparison or if the subquery returns no rows to the outer statement. Otherwise, the expression is unknown. For more information, see SOME | ANY.
EXISTS
Used with a subquery to test for the existence of rows returned by the subquery. For more information, see EXISTS.
Remarks

The order of precedence for the logical operators is NOT (highest), followed by AND, followed by OR. The order of evaluation at the same precedence level is from left to right. Parentheses can be used to override this order in a search condition. For more information about how the logical operators operate on truth values, see AND, OR, and NOT.

Examples
A. Use WHERE with LIKE and ESCAPE syntax

This example assumes a description column exists in finances table. To search for the rows in which the description column contains the exact characters g_, use the ESCAPE option because _ is a wildcard character. Without specifying the ESCAPE option, the query would search for any description values containing the letter g followed by any single character other than the _ character.

SELECT *
FROM finances
WHERE description LIKE 'gs_' ESCAPE 'S'
GO
B. Use WHERE and LIKE syntax with Unicode data

This example uses the WHERE clause to retrieve the contact name, telephone, and fax numbers for any companies containing the string snabbköp at the end of the company name.

USE Northwind
SELECT CompanyName, ContactName, Phone, Fax
FROM Customers
WHERE CompanyName LIKE N'%snabbköp'
ORDER BY CompanyName ASC, ContactName ASC

See Also

Aggregate Functions

CASE

CONTAINSTABLE

Cursors

DELETE

Expressions

FREETEXTTABLE

FROM

Full-text Querying SQL Server Data

Operators (Logical)

UPDATE