Functions for working with strings

[Edit this page](https://github.com/ClickHouse/ClickHouse/tree/master/docs/en/sql-reference/functions/string-functions.md)

Functions for [searching](/docs/sql-reference/functions/string-search-functions) in strings and for [replacing](/docs/sql-reference/functions/string-replace-functions) in strings are described separately.

Note

The documentation below is generated from the `system.functions` system table.

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

Introduced in: v20.1.0

Calculates the CRC32 checksum of a string using the CRC-32-IEEE 802.3 polynomial and initial value `0xffffffff` (zlib implementation).

**Syntax**

```
CRC32(s)
```

**Arguments**

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

**Returned value**

Returns the CRC32 checksum of the string. [`UInt32`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
SELECT CRC32('ClickHouse')
```

```
┌─CRC32('ClickHouse')─┐
│          1538217360 │
└─────────────────────┘
```

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

Introduced in: v20.1.0

Calculates the CRC32 checksum of a string using the CRC-32-IEEE 802.3 polynomial.

**Syntax**

```
CRC32IEEE(s)
```

**Arguments**

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

**Returned value**

Returns the CRC32 checksum of the string. [`UInt32`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
SELECT CRC32IEEE('ClickHouse');
```

```
┌─CRC32IEEE('ClickHouse')─┐
│              3089448422 │
└─────────────────────────┘
```

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

Introduced in: v20.1.0

Calculates the CRC64 checksum of a string using the CRC-64-ECMA polynomial.

**Syntax**

```
CRC64(s)
```

**Arguments**

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

**Returned value**

Returns the CRC64 checksum of the string. [`UInt64`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
SELECT CRC64('ClickHouse');
```

```
┌──CRC64('ClickHouse')─┐
│ 12126588151325169346 │
└──────────────────────┘
```

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

Introduced in: v1.1.0

Appends character `c` to string `s` if `s` is non-empty and does not end with character `c`.

**Syntax**

```
appendTrailingCharIfAbsent(s, c)
```

**Arguments**

- `s` — Input string. [`String`](/docs/sql-reference/data-types/string)
- `c` — Character to append if absent. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns string `s` with character `c` appended if `s` does not end with `c`. [`String`](/docs/sql-reference/data-types/string)

**Examples**

**Usage example**

```
SELECT appendTrailingCharIfAbsent('https://example.com', '/');
```

```
┌─appendTraili⋯.com', '/')─┐
│ https://example.com/     │
└──────────────────────────┘
```

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

Introduced in: v22.11.0

Returns the ASCII code point of the first character of string `s` as an `Int32`.

**Syntax**

```
ascii(s)
```

**Arguments**

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

**Returned value**

Returns the ASCII code point of the first character. If `s` is empty, the result is `0`. If the first character is not an ASCII character or not part of the Latin-1 supplement range of UTF-16, the result is undefined. [`Int32`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
SELECT ascii('234')
```

```
┌─ascii('234')─┐
│           50 │
└──────────────┘
```

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

Introduced in: v25.6.0

Decodes a [Base32](https://datatracker.ietf.org/doc/html/rfc4648#section-6) (RFC 4648) string.
If the string is not valid Base32-encoded, an exception is thrown.

**Syntax**

```
base32Decode(encoded)
```

**Arguments**

- `encoded` — String column or constant. [`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 base32Decode('IVXGG33EMVSA====');
```

```
┌─base32Decode('IVXGG33EMVSA====')─┐
│ Encoded                          │
└──────────────────────────────────┘
```

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

Introduced in: v25.6.0

Encodes a string using [Base32](https://datatracker.ietf.org/doc/html/rfc4648#section-6).

**Syntax**

```
base32Encode(plaintext)
```

**Arguments**

- `plaintext` — Plaintext 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) or [`FixedString`](/docs/sql-reference/data-types/fixedstring)

**Examples**

**Usage example**

```
SELECT base32Encode('Encoded')
```

```
┌─base32Encode('Encoded')─┐
│ IVXGG33EMVSA====        │
└─────────────────────────┘
```

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

Introduced in: v22.7.0

Decodes a [Base58](https://datatracker.ietf.org/doc/html/draft-msporny-base58-03#section-3) string.
If the string is not valid Base58-encoded, an exception is thrown.
An optional second argument `expected_size` can be provided to select an optimized fixed-size decoder.
Currently supported values are 32 and 64. For other values, the generic decoder is used.
When the optimized decoder is selected but the input cannot be decoded to exactly that many bytes,
the function throws an exception (or returns an empty string for `tryBase58Decode`).

**Syntax**

```
base58Decode(encoded[, expected_size])
```

**Arguments**

- `encoded` — String column or constant to decode. [`String`](/docs/sql-reference/data-types/string)
- `expected_size` — Optional. Expected decoded size in bytes. When 32 or 64, an optimized decoder is used; for other values, the generic decoder is used. [`UInt8, UInt16, UInt32, or UInt64`](/docs/sql-reference/data-types/int-uint)

**Returned value**

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

**Examples**

**Usage example**

```
SELECT base58Decode('JxF12TrwUP45BMd');
```

```
┌─base58Decode⋯rwUP45BMd')─┐
│ Hello World              │
└──────────────────────────┘
```

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

Introduced in: v22.7.0

Encodes a string using [Base58](https://tools.ietf.org/id/draft-msporny-base58-01.html) encoding.

**Syntax**

```
base58Encode(plaintext)
```

**Arguments**

- `plaintext` — Plaintext 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 base58Encode('ClickHouse');
```

```
┌─base58Encode('ClickHouse')─┐
│ 4nhk8K7GHXf6zx             │
└────────────────────────────┘
```

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

Introduced in: v18.16.0

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

**Syntax**

```
base64Decode(encoded)
```

**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**