SEARCH

functionsupportedCore SQL functions

docs.snowflake.com ↗

String & binary functions

Searches character data (text) in specified columns from one or more tables, including fields in VARIANT, OBJECT, and ARRAY columns. A text analyzer breaks the text into tokens, which are discrete units of text, such as words or numbers. A default analyzer is applied if you don’t specify one. For more information about using this function, see Using full-text search.

Syntax

supportedpartialnot supportednot scored

SEARCH( <search_data>, '<search_string>'
  [ , ANALYZER => '<analyzer_name>' ]
  [ , SEARCH_MODE => { 'OR' | 'AND' | 'PHRASE' | 'EXACT' } ] )

Parameters

NameRequiredDescription
search_datarequiredThe data you want to search, expressed as a comma-delimited list of string literals, column names, or paths to fields in VARIANT columns. The search data can also be a single literal string, which can be useful when you are testing the function. You can specify the wildcard character (*), where * expands to all qualifying columns in all of the tables that are in scope for the function. Qualifying columns are those that have VARCHAR (text), VARIANT, ARRAY, and OBJECT data types. VARIANT, ARRAY, and OBJECT data is converted to text for searching. When you pass a wildcard to the function, you can qualify the wildcard with the name or alias for the table. For example, to pass in all of the columns from the table named mytable, specify the following: Copy code (mytable.*) You can also use the ILIKE and EXCLUDE keywords for filtering: ILIKE filters for column names that match the specified pattern. Only one pattern is allowed. For example: Copy code (* ILIKE 'col1%') EXCLUDE filters out column names that don’t match the specified column or columns. For example: Copy code (* EXCLUDE col1) (* EXCLUDE (col1, col2)) Qualifiers are valid when you use these keywords. The following example uses the ILIKE keyword to filter for all of the columns that match the pattern col1% in the table mytable: Copy code (mytable.* ILIKE 'col1%') The ILIKE and EXCLUDE keywords can’t be combined in a single function call. For more information about the ILIKE and EXCLUDE keywords, see the “Parameters” section in SELECT. You can search columns from more than one table when multiple tables are in scope by joining tables or using the UNION set operator. To search all of the columns in the output of a join or a UNION query, you can use the unqualified * wildcard as follows:Copy code SELECT * FROM t AS T1 JOIN t AS T2 USING (col1) WHERE SEARCH((*), 'string'); To search specific columns when joining tables, you might need to qualify the column names (for example, table2.colname). You can also use a qualified * wildcard as follows:Copy code SELECT * FROM t AS T1 JOIN t AS T2 USING (col1) WHERE SEARCH((T2.*), 'string'); However, you can’t specify * or table.* more than once for the function. In the previous join example, you can’t specify SEARCH((T1.*, T2.*), 'string'). This syntax returns an error. Parentheses are required for the search_data argument when *, table.*, or multiple items are listed. For example:Copy code SEARCH((col1, col2, col3), 'string') SEARCH((t1.*), 'string') SEARCH((*), 'string') If parentheses aren’t used to separate multiple items, commas are parsed as separators between function arguments. See also Examples of Expected Error Cases. You can search fields in VARIANT data by specifying the column name, a colon or dot, and the subfields separated by dots. For example: colname:fieldname.subfieldname. For more information about specifying fields in such columns, see Traversing Semi-structured Data.
'search_string'requiredA VARCHAR string that contains one or more search terms. This argument must be a literal string; column names aren’t supported. Specify one pair of single quotes around the entire string. Don’t specify quotes around individual terms or phrases. For example, use: 'blue red green' Don’t use: 'blue' 'red' 'green' The list of terms can be disjunctive or conjunctive when OR or AND is set for the SEARCH_MODE argument. However, when the 'NO_OP_ANALYZER' is used, the query string is matched exactly as it is, with no tokenization and no disjunctive or conjunctive semantics. Searches aren’t case sensitive, except when the 'NO_OP_ANALYZER' is used, so a search for the term 'ONCE' against the string 'Once upon a time' returns TRUE. When OR or AND is set for the SEARCH_MODE argument, the order of search terms doesn’t matter with respect to their presence in the searched data. When PHRASE or EXACT is set for the SEARCH_MODE argument, the order of search terms must exactly match the searched data.
ANALYZER => 'analyzer_name'optionalThe name of the text analyzer. The name must be enclosed in single quotes. The analyzer breaks the search terms (and the text from the column being searched) into tokens. The matching semantics (disjunctive, conjunctive, phrase, or exact) for tokens extracted from the search string and tokens extracted from the columns or fields being searched depends on the value of the SEARCH_MODE argument. The analyzer tokenizes a string by breaking it where it finds certain delimiters. These delimiters aren’t included in the resulting tokens, and empty tokens aren’t extracted. This parameter accepts one of the following values: 'DEFAULT_ANALYZER' - Breaks text into tokens based on the following delimiters: Character Unicode code Description `` U+0020 Space [ U+005B Left square bracket ] U+005D Right square bracket ; U+003B Semicolon < U+003C Less-than sign > U+003E Greater-than sign ( U+0028 Left parenthesis ) U+0029 Right parenthesis { U+007B Left curly bracket } U+007D Right curly bracket | U+007C Vertical bar ! U+0021 Exclamation mark , U+002C Comma ' U+0027 Apostrophe " U+0022 Quotation mark * U+002A Asterisk & U+0026 Ampersand ? U+003F Question mark + U+002B Plus sign / U+002F Slash : U+003A Colon = U+003D Equal sign @ U+0040 At sign . U+002E Period (full stop) - U+002D Hyphen $ U+0024 Dollar sign % U+0025 Percent sign \ U+005C Backslash _ U+005F Underscore (low line) \n U+000A New line (line feed) \r U+000D Carriage return \t U+0009 Horizontal tabExpandShow lessSee more 'UNICODE_ANALYZER' - Tokenizes based on Unicode segmentation rules that treat spaces and certain punctuation characters as delimiters. These internal rules are designed for natural language searches in many different languages. For example, the default analyzer treats periods in IP addresses and apostrophes in contractions as delimiters, but the Unicode analyzer doesn’t. See Using an analyzer to adjust search behavior. For more information about the Unicode Text Segmentation algorithm, see https://unicode.org/reports/tr29/. 'NO_OP_ANALYZER' - Tokenizes neither the data nor the query string. A search term must exactly match the full text in a column or field, including case sensitivity; otherwise, the SEARCH function returns FALSE. Even if the query string looks like it contains multiple tokens — for example, 'sky blue' — the column or field must equal the entire query string exactly. In this case, only 'sky blue' is a match; 'sky' and 'blue' aren’t matches. For more information about the behavior of different analyzers, see How search terms are tokenized.
SEARCH_MODE => { 'OR' | 'AND' | 'PHRASE' | 'EXACT' }optionalThe semantics used by the search. Set this argument to one of the following values: 'OR' - The function uses disjunctive semantics. There is a match if any of the tokens extracted from the columns or fields being searched match any of the tokens extracted from the search string. For example, if the search_string value is 'blue red green', the function returns TRUE for a row that contains blue OR red OR green in any of the columns or fields being searched. 'AND' - The function uses conjunctive semantics. There is a match if the tokens extracted from at least one of the columns or fields being searched matches all of the tokens extracted from the search string. The matching tokens must all be in one column or field; they can’t be spread across multiple columns or fields. For example, if the search_string value is 'blue red green', the function returns TRUE for a row that contains blue AND red AND green in at least one of the columns or fields being searched. 'PHRASE' - The function uses phrase-match semantics. There is a match if the tokens extracted from at least one of the columns or fields being searched matches all of the tokens extracted from the search string, including the order and adjacency of the tokens. The matching semantics are the same as conjunctive semantics, except for the following differences: The order of the tokens must exactly match. For example, if the search_string value is 'blue,red,green', the function returns FALSE for red,green,blue. No additional tokens can be interspersed in the search data. For example, if the search_string value is 'blue,red,green', the function returns FALSE for blue,yellow,red,green. 'EXACT' - The function uses exact-match semantics. There is a match if the tokens extracted from at least one of the columns or fields being searched exactly matches all of the tokens extracted from the search string, including the delimiters. The matching rules are the same as phrase-search semantics, except for the following differences: The delimiter strings between the tokens must match exactly. For example, if the search_string value is 'blue,red,green', the function returns TRUE for a row that contains blue,red,green in at least one of the columns or fields being searched. The function returns FALSE for variations such as blue%red%green or blue, red, green. When a delimiter is the first or last character in the search_string value, the delimiter is treated like a character for matching. Therefore, delimiters on the left and right of the first and last delimiter can result in a match. For example, if the search_string value is '[blue]', the function returns TRUE for foo [blue] bar, [[blue]], and =[blue]., but not for (blue) or foo blue bar. For all search modes, the string must be delimited by a delimiter symbol on the left and right. For example, if the search_string value is 'blue,red,green', the function returns TRUE for -blue,red,green;. The function returns FALSE for darkblue,red,green or blue,red,greenish. If you use the UNICODE_ANALYZER, exact-match semantics aren’t supported. You can use the DEFAULT_ANALYZER or the NO_OP_ANALYZER with exact-match semantics, but generally these search semantics are best suited for the DEFAULT_ANALYZER. A search that uses exact-match semantics with the DEFAULT_ANALYZER behaves differently than an equality search or a full-text searches with the NO_OP_ANALYZER in the following ways: An equality search matches rows where the column value is exactly the same as in the predicate (including letter case), with no additional text surrounding the occurrence of the search string. A full-text search with the NO_OP_ANALYZER is similar to an equality search in that it is case-sensitive and doesn’t allow additional text. A search with exact-match semantics with the DEFAULT_ANALYZER tokenizes the column values. It allows additional tokens to the left and right of the search string occurrence as long as they are separated by token delimiters. The search is case-insensitive. Default: 'OR'

Returns

Returns a BOOLEAN: - The value is TRUE if search_string tokens match search_data tokens based on the semantics specified in the SEARCH_MODE argument. - Returns NULL if either of these arguments is NULL. - Otherwise, returns FALSE.

Usage notes

How search terms are tokenized

The following table shows a few examples of how input search terms are split into tokens, which depends on the rules applied by the analyzer that is used. In the table, commas denote where the tokens are split (if they are). Search Term(s) | Tokens: DEFAULT_ANALYZER | Tokens: UNICODE_ANALYZER | NO_OP_ANALYZER (not split) 10.210.158.44 | 10, 210, 158, 44 | 10.210.158.44 | 10.210.158.44 192.0.2.0/24 | 192, 0, 2, 24 | 192.0.2.0, 24 | 192.0.2.0/24 high-tech | high, tech | high, tech | high-tech Bob's Burgers | bob, s, burgers | bob's, burgers | Bob's Burgers Three spaces | three, spaces | three, spaces | Three spaces docs@snowflake.com | docs, snowflake, com | docs, snowflake.com | docs@snowflake.com /opt/homebrew/README.md | opt, homebrew, readme, md | opt, homebrew, readme.md | /opt/homebrew/README.md