CONTAINS Conditions

Perform a full text query using the CONTAINS clause in the FROM clause of a SELECT statement, or by using the CONTAINS search condition (predicate) in a WHERE clause.

Both methods return the same rows; however, the CONTAINS clause also returns scores for the matching rows.

Syntax

CONTAINS ( column-name [,...], contains-query-string )

contains-query-string:
   simple-expression | or-expression

simple-expression:
   primary-expression | and-expression

or-expression:
   simple-expression { OR | | } contains-query-string


primary-expression:
   basic-expression
   | FUZZY " fuzzy-expression "
   | and-not-expression

and-expression:
   primary-expression [ AND | & ] simple-expression

and-not-expression:
   primary-expression [ AND | & ] 
   { NOT | - } basic-expression

basic-expression:
   term
   | phrase
   | ( contains-query-string )
   | proximity-expression

fuzzy-expression:
   term | fuzzy-expression term


term:
   simple-term | prefix-term

prefix-term: 
   simple-term*

phrase:
   " phrase-string "

proximity-expression:
   term ( BEFORE | NEAR ) 
   [ minimum distance, | maximum distance ] term | term 
   {BEFORE | NEAR | ~ } term

phrase-string:
	term | phrase-string term

Parameters

Usage

The CONTAINS search condition takes a column list and contains-query-string as arguments.

The CONTAINS search condition can be used anywhere a search condition (also referred to as predicate) can be specified, and returns TRUE or FALSE. contains-query-string must be a constant string, or a variable, with a value that is known at query time.

If multiple columns are specified, then they must all refer to a single base table; a TEXT index cannot span multiple base tables. You can reference the base directly in the FROM clause, or use it in a view or derived table, provided that the view or derived table does not use DISTINCT, GROUP BY, ORDER BY, UNION, INTERSECT, EXCEPT, or a row limitation.

Queries using ANSI join syntax are supported (FULL OUTER JOIN, RIGHT OUTER JOIN, LEFT OUTER JOIN), but may have suboptimal performance. Use outer joins for CONTAINS in the FROM clause only if the score column from each of the CONTAINS clauses is required. Otherwise, move CONTAINS to an ON condition or WHERE clause.

These types of queries are unsupported:
  • Remote queries using a SQL Anywhere table with a full TEXT index that is joined to a remote table.
  • Queries using SAP Sybase IQ and SQL Anywhere tables, where the full TEXT index to be used is on the SQL Anywhere table.
  • Queries using TSQL style outer join syntax (*=*, =* and *=).

If you use a SQL variable less than 32KB in length as a search term and the type of variable is LONG VARCHAR, use CAST to convert the variable to VARCHAR data type. For example:

SELECT * FROM tab1 WHERE CONTAINS(c1, cast(v1 AS VARCHAR(64))
The following warnings apply to the use of non-alphanumeric characters in query strings:
  • An asterisk in the middle of a term returns an error.
  • Avoid using non-alphanumeric characters (including special characters) in fuzzy-expression, because they are treated as white space and serve as term breakers.
  • If possible, avoid using non-alphanumeric characters that are not special characters in your query string. Any non-alphanumeric character that is not a special character causes the term containing it to be treated as a phrase, breaking the term at the location of the character. For example, 'things we've done' is interpreted as 'things "we ve" done'.

Within phrases, the asterisk is the only special character that continues to be interpreted as a special character. All other special characters within phrases are treated as white space and serve as term breakers.

Interpretation of contains-query-string takes place in two main steps:
  • Step 1: Interpretation of operators and precedence: During this step, keywords are interpreted as operators, and rules of precedence are applied.
  • Step 2: Application of text configuration object settings: During this step, the text configuration object settings are applied to terms. Any query terms that exceed the term length settings, or that are in the stop list, are dropped.
Related concepts
Fuzzy Search Over a TEXT Index