Skip to main content

search_substring function

Applies to: check marked yes Databricks SQL check marked yes Databricks Runtime 19

Beta

This feature is in Beta.

Returns true if every term in search_pattern occurs as a substring within one string-searchable value in expr.

Syntax​

search_substring(expr, search_pattern [, search_pattern ...] [, case => case_sensitivity])

Arguments​

  • expr: The expression to search. Only one target expression is supported.
  • search_pattern: One or more STRING constant expressions, each containing a term to search for. You can pass up to 5 search patterns per call. To search for more, combine several calls with AND.
  • case_sensitivity: An optional STRING literal that controls case sensitivity. Valid values are 'sensitive' and 'insensitive', regardless of letter case. The default is 'sensitive'.

Returns​

A BOOLEAN value that indicates whether every search term occurs within one string-searchable value in expr.

Notes​

With case => 'insensitive', matching follows the case-insensitive rules of the UTF8_LCASE collation. Input strings must still use the default UTF8_BINARY collation.

The function matches each term literally. Characters such as ., *, and % have no special meaning as regular expression or wildcard characters.

An empty string is a valid term and matches every non-null string value.

When you pass multiple search_pattern arguments, every term must occur within the same string-searchable value. Terms can occur in any order and at any distance from each other.

The following types are string-searchable:

Type

String-searchable when

VOID, VARIANT

Always.

STRING

Only when it uses the default UTF8_BINARY collation. The function ignores STRING values with any other collation. See Collation.

ARRAY

Its child expression is string-searchable.

MAP

Its value expression is string-searchable.

STRUCT

At least one of its field expressions is string-searchable.

Type

String-searchable when

VOID, VARIANT

Always.

STRING

Only when it uses the default UTF8_BINARY collation. The function ignores STRING values with any other collation. See Collation.

ARRAY

Its child expression is string-searchable.

MAP

Its value expression is string-searchable.

STRUCT

At least one of its field expressions is string-searchable.

For STRUCT expressions, search_substring ignores fields that are not string-searchable. For VARIANT expressions, it searches only leaf nodes of type VOID or STRING.

A STRING column declared without a COLLATE clause uses its table's default collation, which the table can inherit from its schema. If that collation is not UTF8_BINARY, search_substring ignores the column, so a search over only that column returns false. To search the column, cast it to STRING COLLATE UTF8_BINARY. Full-text search indexes don't support columns with other collations, so a search over the cast column doesn't use an index.

The function does not implicitly cast non-STRING expressions to STRING expressions.

The return value is determined as follows:

  • NULL if a search_pattern is NULL.
  • true if at least one string-searchable value matches every term.
  • NULL if no string-searchable value matches every term and at least one string-searchable value is NULL.
  • false otherwise.

Queries that use search_substring can benefit from a full-text search index that uses the ngram tokenizer. See Full-text search indexes on Unity Catalog managed tables.

Common error conditions​

Examples​

SQL
-- Match a literal substring.
> SELECT search_substring(column, 'needle')
FROM VALUES ('needlework kit'), ('haystack') AS table(column);
true
false

-- Pass several search patterns. Every term must match within one value.
> SELECT search_substring('prefix foo.bar with 100% coverage', 'foo.bar', '100%');
true

> SELECT search_substring('prefix foo.bar with 100% coverage', 'foo.bar', 'missing');
false

-- Ignore letter case.
> SELECT search_substring('Needle in Haystack', 'needle', case => 'insensitive');
true

-- Compute a constant search term from an expression.
> SELECT search_substring('needlework kit', lower('NEEDLE'));
true

-- Terms in separate VARIANT leaf values do not match.
> SELECT search_substring(parse_json('{"role":"user","theme":"dark"}'), 'user', 'dark');
false

-- A column with a default collation other than UTF8_BINARY is ignored.
> CREATE TABLE notes (body STRING) DEFAULT COLLATION UTF8_LCASE;
> INSERT INTO notes VALUES ('asked about pricing');
> SELECT search_substring(body, 'pricing') FROM notes;
false

-- Cast the column to UTF8_BINARY to search it.
> SELECT search_substring(CAST(body AS STRING COLLATE UTF8_BINARY), 'pricing') FROM notes;
true

-- NULL behavior.
> SELECT search_substring(CAST(NULL AS STRING), 'needle');
NULL