Functions for Searching in Strings

**Returned value**

Returns `1` if the phrase is found as a consecutive token sequence, `0` otherwise. [`UInt8`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Phrase match**

```
SELECT hasPhrase('the quick brown fox jumps', 'quick brown')
```

```
┌─hasPhrase('the quick brown fox jumps', 'quick brown')─┐
│                                                      1 │
└────────────────────────────────────────────────────────┘
```

**Non-consecutive tokens**

```
SELECT hasPhrase('the quick brown fox jumps', 'quick fox')
```

```
┌─hasPhrase('the quick brown fox jumps', 'quick fox')─┐
│                                                    0 │
└──────────────────────────────────────────────────────┘
```

## hasSubsequence[​](#hasSubsequence "Direct link to hasSubsequence")

Introduced in: v23.7.0

Checks if a needle is a subsequence of a haystack.
A subsequence of a string is a sequence that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.

**Syntax**

```
hasSubsequence(haystack, needle)
```

**Arguments**

- `haystack` — String in which to search for the subsequence. [`String`](/docs/sql-reference/data-types/string)
- `needle` — Subsequence to be searched. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns `1` if needle is a subsequence of haystack, `0` otherwise. [`UInt8`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Basic subsequence check**

```
SELECT hasSubsequence('Hello World', 'HlWrd')
```

```
┌─hasSubsequence('Hello World', 'HlWrd')─┐
│                                      1 │
└────────────────────────────────────────┘
```

**No subsequence found**

```
SELECT hasSubsequence('Hello World', 'xyz')
```

```
┌─hasSubsequence('Hello World', 'xyz')─┐
│                                    0 │
└──────────────────────────────────────┘
```

## hasSubsequenceCaseInsensitive[​](#hasSubsequenceCaseInsensitive "Direct link to hasSubsequenceCaseInsensitive")

Introduced in: v23.7.0

Like [`hasSubsequence`](#hasSubsequence) but searches case-insensitively.

**Syntax**

```
hasSubsequenceCaseInsensitive(haystack, needle)
```

**Arguments**

- `haystack` — String in which the search is performed. [`String`](/docs/sql-reference/data-types/string)
- `needle` — Subsequence to be searched. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns 1, if needle is a subsequence of haystack, 0 otherwise. [`UInt8`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
SELECT hasSubsequenceCaseInsensitive('garbage', 'ARG');
```

```
┌─hasSubsequenceCaseInsensitive('garbage', 'ARG')─┐
│                                               1 │
└─────────────────────────────────────────────────┘
```

## hasSubsequenceCaseInsensitiveUTF8[​](#hasSubsequenceCaseInsensitiveUTF8 "Direct link to hasSubsequenceCaseInsensitiveUTF8")

Introduced in: v23.7.0

Like [`hasSubsequenceUTF8`](#hasSubsequenceUTF8) but searches case-insensitively.

**Syntax**

```
hasSubsequenceCaseInsensitiveUTF8(haystack, needle)
```

**Arguments**

- `haystack` — UTF8-encoded string in which the search is performed. [`String`](/docs/sql-reference/data-types/string)
- `needle` — UTF8-encoded subsequence string to be searched. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns 1, if needle is a subsequence of haystack, 0 otherwise. [`UInt8`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
SELECT hasSubsequenceCaseInsensitiveUTF8('ClickHouse - столбцовая система управления базами данных', 'СИСТЕМА');
```

```
┌─hasSubsequen⋯ 'СИСТЕМА')─┐
│                        1 │
└──────────────────────────┘
```

## hasSubsequenceUTF8[​](#hasSubsequenceUTF8 "Direct link to hasSubsequenceUTF8")

Introduced in: v23.7.0

Like [`hasSubsequence`](/docs/sql-reference/functions/string-search-functions#hasSubsequence) but assumes haystack and needle are UTF-8 encoded strings.

**Syntax**

```
hasSubsequenceUTF8(haystack, needle)
```

**Arguments**

- `haystack` — The string in which to search. [`String`](/docs/sql-reference/data-types/string)
- `needle` — The subsequence to search for. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns `1` if `needle` is a subsequence of `haystack`, otherwise `0`. [`UInt8`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
SELECT hasSubsequenceUTF8('картошка', 'кошка');
```

```
┌─hasSubsequen⋯', 'кошка')─┐
│                        1 │
└──────────────────────────┘
```

**Non-matching subsequence**

```
SELECT hasSubsequenceUTF8('картошка', 'апельсин');
```

```
┌─hasSubsequen⋯'апельсин')─┐
│                        0 │
└──────────────────────────┘
```

## hasToken[​](#hasToken "Direct link to hasToken")

Introduced in: v20.1.0

Checks if the given token is present in the haystack.

Uses [splitByNonAlpha](/docs/sql-reference/functions/splitting-merging-functions#splitByNonAlpha) as tokenizer, i.e. a token is defined as the longest possible sub-sequence of consecutive characters `[0-9A-Za-z_]` (numbers, ASCII characters and underscore).

**Syntax**

```
hasToken(haystack, token)
```

**Arguments**

- `haystack` — String to be searched. [`String`](/docs/sql-reference/data-types/string)
- `token` — Token to search for. [`const String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns `1` if the token is found, `0` otherwise. [`UInt8`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Token search**

```
SELECT hasToken('clickhouse test', 'test')
```

```
┌─hasToken('clickhouse test', 'test')─┐
│                                   1 │
└─────────────────────────────────────┘
```

## hasTokenCaseInsensitive[​](#hasTokenCaseInsensitive "Direct link to hasTokenCaseInsensitive")

Introduced in: v20.1.0

Performs case insensitive lookup of needle in haystack using tokenbf\_v1 index.

**Syntax**

```
hasTokenCaseInsensitive(haystack, needle)
```

**Arguments**

- None.

**Returned value**

**Examples**

## hasTokenCaseInsensitiveOrNull[​](#hasTokenCaseInsensitiveOrNull "Direct link to hasTokenCaseInsensitiveOrNull")

Introduced in: v23.1.0

Performs case insensitive lookup of needle in haystack using tokenbf\_v1 index. Returns null if needle is ill-formed.

**Syntax**

```
hasTokenCaseInsensitiveOrNull(haystack, needle)
```

**Arguments**

- None.

**Returned value**

**Examples**

## hasTokenOrNull[​](#hasTokenOrNull "Direct link to hasTokenOrNull")

Introduced in: v20.1.0

Like [`hasToken`](#hasToken) but returns null if token is ill-formed.

**Syntax**

```
hasTokenOrNull(haystack, token)
```

**Arguments**

- `haystack` — String to be searched. Must be constant. [`String`](/docs/sql-reference/data-types/string)
- `token` — Token to search for. [`const String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns `1` if the token is found, `0` otherwise, null if token is ill-formed. [`Nullable(UInt8)`](/docs/sql-reference/data-types/nullable)

**Examples**

**Usage example**

```
SELECT hasTokenOrNull('apple banana cherry', 'ban ana');
```

```
┌─hasTokenOrNu⋯ 'ban ana')─┐
│                     ᴺᵁᴸᴸ │
└──────────────────────────┘
```

## highlight[​](#highlight "Direct link to highlight")

Introduced in: v26.4.0

Highlights occurrences of search terms in a text string by wrapping them with HTML tags.

The function performs ASCII case-insensitive matching. If multiple search terms overlap or are adjacent in the text, the matched regions are merged into a single highlighted span.

**Syntax**

```
highlight(haystack, needles[, open_tag, close_tag])
```

**Arguments**