Functions for working with strings

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

Introduced in: v24.1.0

Calculates the [Damerau-Levenshtein distance](https://en.wikipedia.org/wiki/Damerau%E2%80%93Levenshtein_distance) between two byte strings.

**Syntax**

```
damerauLevenshteinDistance(s1, s2)
```

**Arguments**

- `s1` — First input string. [`String`](/docs/sql-reference/data-types/string)
- `s2` — Second input string. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns the Damerau-Levenshtein distance between the two strings. [`UInt64`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
SELECT damerauLevenshteinDistance('clickhouse', 'mouse')
```

```
┌─damerauLevenshteinDistance('clickhouse', 'mouse')─┐
│                                                 6 │
└───────────────────────────────────────────────────┘
```

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

Introduced in: v23.9.0

Decodes HTML entities in a string to their corresponding characters.

**Syntax**

```
decodeHTMLComponent(s)
```

**Arguments**

- `s` — String containing HTML entities to decode. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns the string with HTML entities decoded. [`String`](/docs/sql-reference/data-types/string)

**Examples**

**Usage example**

```
SELECT decodeHTMLComponent('&lt;div&gt;Hello &amp; &quot;World&quot;&lt;/div&gt;')
```

```
┌─decodeHTMLComponent('&lt;div&gt;Hello &amp; &quot;World&quot;&lt;/div&gt;')─┐
│ <div>Hello & "World"</div>                                                  │
└─────────────────────────────────────────────────────────────────────────────┘
```

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

Introduced in: v21.2.0

Decodes XML entities in a string to their corresponding characters.

**Syntax**

```
decodeXMLComponent(s)
```

**Arguments**

- `s` — String containing XML entities to decode. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns the provided string with XML entities decoded. [`String`](/docs/sql-reference/data-types/string)

**Examples**

**Usage example**

```
SELECT decodeXMLComponent('&lt;tag&gt;Hello &amp; World&lt;/tag&gt;')
```

```
┌─decodeXMLCom⋯;/tag&gt;')─┐
│ <tag>Hello & World</tag> │
└──────────────────────────┘
```

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

Introduced in: v23.9.0

Calculates the [edit distance](https://en.wikipedia.org/wiki/Edit_distance) between two byte strings.

**Syntax**

```
editDistance(s1, s2)
```

**Aliases**: `levenshteinDistance`

**Arguments**

- `s1` — First input string. [`String`](/docs/sql-reference/data-types/string)
- `s2` — Second input string. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns the edit distance between the two strings. [`UInt64`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
SELECT editDistance('clickhouse', 'mouse')
```

```
┌─editDistance('clickhouse', 'mouse')─┐
│                                   6 │
└─────────────────────────────────────┘
```

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

Introduced in: v24.6.0

Calculates the [edit distance](https://en.wikipedia.org/wiki/Edit_distance) between two UTF8 strings.

**Syntax**

```
editDistanceUTF8(s1, s2)
```

**Aliases**: `levenshteinDistanceUTF8`

**Arguments**

- `s1` — First input string. [`String`](/docs/sql-reference/data-types/string)
- `s2` — Second input string. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns the edit distance between the two UTF8 strings. [`UInt64`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
SELECT editDistanceUTF8('我是谁', '我是我')
```

```
┌─editDistanceUTF8('我是谁', '我是我')──┐
│                                   1 │
└─────────────────────────────────────┘
```

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

Introduced in: v21.1.0

Escapes characters to place string into XML text node or attribute.

**Syntax**

```
encodeXMLComponent(s)
```

**Arguments**

- `s` — String to escape. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns the escaped string. [`String`](/docs/sql-reference/data-types/string)

**Examples**

**Usage example**

```
SELECT
    '<tag>Hello & "World"</tag>' AS original,
    encodeXMLComponent('<tag>Hello & "World"</tag>') AS xml_encoded;
```

```
┌─original───────────────────┬─xml_encoded──────────────────────────────────────────┐
│ <tag>Hello & "World"</tag> │ &lt;tag&gt;Hello &amp; &quot;World&quot;&lt;/tag&gt; │
└────────────────────────────┴──────────────────────────────────────────────────────┘
```

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

Introduced in: v1.1.0

Checks whether a string ends with the provided suffix.

**Syntax**

```
endsWith(s, suffix)
```

**Arguments**

- `s` — String to check. [`String`](/docs/sql-reference/data-types/string)
- `suffix` — Suffix to check for. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns `1` if `s` ends with `suffix`, otherwise `0`. [`UInt8`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
SELECT endsWith('ClickHouse', 'House');
```

```
┌─endsWith('Cl⋯', 'House')─┐
│                        1 │
└──────────────────────────┘
```

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

Introduced in: v25.9.0

Checks whether a string ends with the provided case-insensitive suffix.

**Syntax**

```
endsWithCaseInsensitive(s, suffix)
```

**Arguments**

- `s` — String to check. [`String`](/docs/sql-reference/data-types/string)
- `suffix` — Case-insensitive suffix to check for. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns `1` if `s` ends with case-insensitive `suffix`, otherwise `0`. [`UInt8`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
SELECT endsWithCaseInsensitive('ClickHouse', 'HOUSE');
```

```
┌─endsWithCaseInsensitive('Cl⋯', 'HOUSE')─┐
│                                       1 │
└─────────────────────────────────────────┘
```

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

Introduced in: v25.9.0

Returns whether string `s` ends with case-insensitive `suffix`.
Assumes that the string contains valid UTF-8 encoded text.
If this assumption is violated, no exception is thrown and the result is undefined.

**Syntax**

```
endsWithCaseInsensitiveUTF8(s, suffix)
```

**Arguments**

- `s` — String to check. [`String`](/docs/sql-reference/data-types/string)
- `suffix` — Case-insensitive suffix to check for. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns `1` if `s` ends with case-insensitive `suffix`, otherwise `0`. [`UInt8`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
SELECT endsWithCaseInsensitiveUTF8('данных', 'ых');
```

```
┌─endsWithCaseInsensitiveUTF8('данных', 'ых')─┐
│                                           1 │
└─────────────────────────────────────────────┘
```

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

Introduced in: v23.8.0

Returns whether string `s` ends with `suffix`.
Assumes that the string contains valid UTF-8 encoded text.
If this assumption is violated, no exception is thrown and the result is undefined.

**Syntax**

```
endsWithUTF8(s, suffix)
```

**Arguments**

- `s` — String to check. [`String`](/docs/sql-reference/data-types/string)
- `suffix` — Suffix to check for. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns `1` if `s` ends with `suffix`, otherwise `0`. [`UInt8`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
SELECT endsWithUTF8('данных', 'ых');
```

```
┌─endsWithUTF8('данных', 'ых')─┐
│                            1 │
└──────────────────────────────┘
```