Functions for working with strings

**Aliases**: `FROM_BASE64`

**Arguments**

- `encoded` — String column or constant to decode. If the string is not valid Base64-encoded, an exception is thrown. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

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

**Examples**

**Usage example**

```
SELECT base64Decode('Y2xpY2tob3VzZQ==')
```

```
┌─base64Decode('Y2xpY2tob3VzZQ==')─┐
│ clickhouse                       │
└──────────────────────────────────┘
```

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

Introduced in: v18.16.0

Encodes a string using [Base64](https://en.wikipedia.org/wiki/Base64) representation, according to RFC 4648.

**Syntax**

```
base64Encode(plaintext)
```

**Aliases**: `TO_BASE64`

**Arguments**

- `plaintext` — Plaintext column or constant to decode. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns a string containing the encoded value of the argument. [`String`](/docs/sql-reference/data-types/string)

**Examples**

**Usage example**

```
SELECT base64Encode('clickhouse')
```

```
┌─base64Encode('clickhouse')─┐
│ Y2xpY2tob3VzZQ==           │
└────────────────────────────┘
```

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

Introduced in: v24.6.0

Decodes a string from [Base64](https://en.wikipedia.org/wiki/Base64) representation using URL-safe alphabet, according to RFC 4648.
Throws an exception in case of error.

**Syntax**

```
base64URLDecode(encoded)
```

**Arguments**

- `encoded` — String column or constant to encode. If the string is not valid Base64-encoded, an exception is thrown. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns a string containing the decoded value of the argument. [`String`](/docs/sql-reference/data-types/string)

**Examples**

**Usage example**

```
SELECT base64URLDecode('aHR0cHM6Ly9jbGlja2hvdXNlLmNvbQ')
```

```
┌─base64URLDecode('aHR0cHM6Ly9jbGlja2hvdXNlLmNvbQ')─┐
│ https://clickhouse.com                            │
└───────────────────────────────────────────────────┘
```

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

Introduced in: v18.16.0

Encodes a string using [Base64](https://datatracker.ietf.org/doc/html/rfc4648#section-4) (RFC 4648) representation using URL-safe alphabet.

**Syntax**

```
base64URLEncode(plaintext)
```

**Arguments**

- `plaintext` — Plaintext column or constant to encode. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns a string containing the encoded value of the argument. [`String`](/docs/sql-reference/data-types/string)

**Examples**

**Usage example**

```
SELECT base64URLEncode('https://clickhouse.com')
```

```
┌─base64URLEncode('https://clickhouse.com')─┐
│ aHR0cHM6Ly9jbGlja2hvdXNlLmNvbQ            │
└───────────────────────────────────────────┘
```

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

Introduced in: v20.1.0

Extracts the tail of a string following its last slash or backslash.
This function is often used to extract the filename from a path.

**Syntax**

```
basename(expr)
```

**Arguments**

- `expr` — A string expression. Backslashes must be escaped. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns the tail of the input string after its last slash or backslash. If the input string ends with a slash or backslash, the function returns an empty string. Returns the original string if there are no slashes or backslashes. [`String`](/docs/sql-reference/data-types/string)

**Examples**

**Extract filename from Unix path**

```
SELECT 'some/long/path/to/file' AS a, basename(a)
```

```
┌─a──────────────────────┬─basename('some/long/path/to/file')─┐
│ some/long/path/to/file │ file                               │
└────────────────────────┴────────────────────────────────────┘
```

**Extract filename from Windows path**

```
SELECT 'some\\long\\path\\to\\file' AS a, basename(a)
```

```
┌─a──────────────────────┬─basename('some\\long\\path\\to\\file')─┐
│ some\long\path\to\file │ file                                   │
└────────────────────────┴────────────────────────────────────────┘
```

**String with no path separators**

```
SELECT 'some-file-name' AS a, basename(a)
```

```
┌─a──────────────┬─basename('some-file-name')─┐
│ some-file-name │ some-file-name             │
└────────────────┴────────────────────────────┘
```

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

Introduced in: v23.9.0

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

**Syntax**

```
byteHammingDistance(s1, s2)
```

**Aliases**: `mismatches`

**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 Hamming distance between the two strings. [`UInt64`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
SELECT byteHammingDistance('karolin', 'kathrin')
```

```
┌─byteHammingDistance('karolin', 'kathrin')─┐
│                                         3 │
└───────────────────────────────────────────┘
```

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

Introduced in: v26.3.0

Applies Unicode case folding to a UTF-8 string, converting it to a lowercase-like normalized form suitable for case-insensitive comparisons.

Applies standard Unicode case folding. Preserves compatibility characters that are not affected by case folding
(e.g. Roman numerals, circled numbers), but note that some ligatures like `ﬃ` are still decomposed because Unicode case folding itself expands them.

**Syntax**

```
caseFoldUTF8(str)
```

**Arguments**

- `str` — UTF-8 encoded input string. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Case-folded UTF-8 string. [`String`](/docs/sql-reference/data-types/string)

**Examples**

**Basic case folding**

```
SELECT caseFoldUTF8('Straße')
```

```
┌─caseFoldUTF8('Straße')─┐
│ strasse                 │
└─────────────────────────┘
```

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

Introduced in: v25.2.0

Compares two strings lexicographically.

**Syntax**

```
compareSubstrings(s1, s2, s1_offset, s2_offset, num_bytes)
```

**Arguments**

- `s1` — The first string to compare. [`String`](/docs/sql-reference/data-types/string)
- `s2` — The second string to compare. [`String`](/docs/sql-reference/data-types/string)
- `s1_offset` — The position (zero-based) in `s1` from which the comparison starts. [`UInt*`](/docs/sql-reference/data-types/int-uint)
- `s2_offset` — The position (zero-based index) in `s2` from which the comparison starts. [`UInt*`](/docs/sql-reference/data-types/int-uint)
- `num_bytes` — The maximum number of bytes to compare in both strings. If `s1_offset` (or `s2_offset`) + `num_bytes` exceeds the end of an input string, `num_bytes` will be reduced accordingly. [`UInt*`](/docs/sql-reference/data-types/int-uint)

**Returned value**

Returns:

- `-1` if `s1`[`s1_offset` : `s1_offset` + `num_bytes`] < `s2`[`s2_offset` : `s2_offset` + `num_bytes`].
- `0` if `s1`[`s1_offset` : `s1_offset` + `num_bytes`] = `s2`[`s2_offset` : `s2_offset` + `num_bytes`].
- `1` if `s1`[`s1_offset` : `s1_offset` + `num_bytes`] > `s2`[`s2_offset` : `s2_offset` + `num_bytes`].
  [`Int8`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
SELECT compareSubstrings('Saxony', 'Anglo-Saxon', 0, 6, 5) AS result
```

```
┌─result─┐
│      0 │
└────────┘
```

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

Introduced in: v1.1.0

Concatenates the given arguments.

Arguments which are not of types [`String`](/docs/sql-reference/data-types/string) or [`FixedString`](/docs/sql-reference/data-types/fixedstring) are converted to strings using their default serialization.
As this decreases performance, it is not recommended to use non-String/FixedString arguments.

**Syntax**

```
concat([s1, s2, ...])
```

**Arguments**