Functions for working with strings

**Returned value**

Returns a right-padded string of the given length. [`String`](/docs/sql-reference/data-types/string)

**Examples**

**Usage example**

```
SELECT rightPadUTF8('абвг', 7, '*'), rightPadUTF8('абвг', 7)
```

```
┌─rightPadUTF8('абвг', 7, '*')─┬─rightPadUTF8('абвг', 7)─┐
│ абвг***                      │ абвг                    │
└──────────────────────────────┴─────────────────────────┘
```

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

Introduced in: v22.1.0

Returns a substring of UTF-8 encoded string `s` with a specified `offset` starting from the right.

**Syntax**

```
rightUTF8(s, offset)
```

**Arguments**

- `s` — The UTF-8 encoded string to calculate a substring from. [`String`](/docs/sql-reference/data-types/string) or [`FixedString`](/docs/sql-reference/data-types/fixedstring)
- `offset` — The number of bytes of the offset. [`(U)Int*`](/docs/sql-reference/data-types/int-uint)

**Returned value**

Returns:

- For positive `offset`, a substring of `s` with `offset` many bytes, starting from the right of the string.
- For negative `offset`, a substring of `s` with `length(s) - |offset|` bytes, starting from the right of the string.
- An empty string if `length` is `0`.
  [`String`](/docs/sql-reference/data-types/string)

**Examples**

**Positive offset**

```
SELECT rightUTF8('Привет', 4)
```

```
ивет
```

**Negative offset**

```
SELECT rightUTF8('Привет', -4)
```

```
ет
```

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

Introduced in: v23.4.0

Returns the [Soundex code](https://en.wikipedia.org/wiki/Soundex) of a string.

**Syntax**

```
soundex(s)
```

**Arguments**

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

**Returned value**

Returns the Soundex code of the input string. [`String`](/docs/sql-reference/data-types/string)

**Examples**

**Usage example**

```
SELECT soundex('aksel')
```

```
┌─soundex('aksel')─┐
│ A240             │
└──────────────────┘
```

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

Introduced in: v23.5.0

Concatenates a space () as many times with itself as specified.

**Syntax**

```
space(n)
```

**Arguments**

- `n` — The number of times to repeat the space. [`(U)Int*`](/docs/sql-reference/data-types/int-uint)

**Returned value**

Returns astring containing a space repeated `n` times. If `n <= 0`, the function returns the empty string. [`String`](/docs/sql-reference/data-types/string)

**Examples**

**Usage example**

```
SELECT space(3) AS res, length(res);
```

```
┌─res─┬─length(res)─┐
│     │           3 │
└─────┴─────────────┘
```

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

Introduced in: v25.5.0

Finds all substrings of a given string that have a length of at least `n`,
where the hashes of the (n-1)-grams at the borders of the substring
are strictly greater than those of any (n-1)-gram inside the substring.
Uses `CRC32` as a hash function.

**Syntax**

```
sparseGrams(s[, min_ngram_length[, max_ngram_length[, min_cutoff_length]]])
```

**Arguments**

- `s` — An input string. [`String`](/docs/sql-reference/data-types/string)
- `min_ngram_length` — Optional. The minimum length of extracted ngram. The default and minimal value is 3. [`UInt*`](/docs/sql-reference/data-types/int-uint)
- `max_ngram_length` — Optional. The maximum length of extracted ngram. The default value is 100. Should be not less than `min_ngram_length`. [`UInt*`](/docs/sql-reference/data-types/int-uint)
- `min_cutoff_length` — Optional. If specified, only n-grams with length greater or equal than `min_cutoff_length` are returned. The default value is the same as `min_ngram_length`. Should be not less than `min_ngram_length` and not greater than `max_ngram_length`. [`UInt*`](/docs/sql-reference/data-types/int-uint)

**Returned value**

Returns an array of selected substrings. [`Array(String)`](/docs/sql-reference/data-types/array)

**Examples**

**Usage example**

```
SELECT sparseGrams('alice', 3)
```

```
┌─sparseGrams('alice', 3)────────────┐
│ ['ali','lic','lice','ice']         │
└────────────────────────────────────┘
```

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

Introduced in: v25.5.0

Finds hashes of all substrings of a given string that have a length of at least `n`,
where the hashes of the (n-1)-grams at the borders of the substring
are strictly greater than those of any (n-1)-gram inside the substring.
Uses `CRC32` as a hash function.

**Syntax**

```
sparseGramsHashes(s[, min_ngram_length, max_ngram_length])
```

**Arguments**

- `s` — An input string. [`String`](/docs/sql-reference/data-types/string)
- `min_ngram_length` — Optional. The minimum length of extracted ngram. The default and minimal value is 3. [`UInt*`](/docs/sql-reference/data-types/int-uint)
- `max_ngram_length` — Optional. The maximum length of extracted ngram. The default value is 100. Should be not less than `min_ngram_length`. [`UInt*`](/docs/sql-reference/data-types/int-uint)
- `min_cutoff_length` — Optional. If specified, only n-grams with length greater or equal than `min_cutoff_length` are returned. The default value is the same as `min_ngram_length`. Should be not less than `min_ngram_length` and not greater than `max_ngram_length`. [`UInt*`](/docs/sql-reference/data-types/int-uint)

**Returned value**

Returns an array of selected substrings CRC32 hashes. [`Array(UInt32)`](/docs/sql-reference/data-types/array)

**Examples**

**Usage example**

```
SELECT sparseGramsHashes('alice', 3)
```

```
┌─sparseGramsHashes('alice', 3)──────────────────────┐
│ [1481062250,2450405249,4012725991,1918774096]      │
└────────────────────────────────────────────────────┘
```

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

Introduced in: v25.5.0

Finds hashes of all substrings of a given UTF-8 string that have a length of at least `n`, where the hashes of the (n-1)-grams at the borders of the substring are strictly greater than those of any (n-1)-gram inside the substring.
Expects UTF-8 string, throws an exception in case of invalid UTF-8 sequence.
Uses `CRC32` as a hash function.

**Syntax**

```
sparseGramsHashesUTF8(s[, min_ngram_length, max_ngram_length])
```

**Arguments**

- `s` — An input string. [`String`](/docs/sql-reference/data-types/string)
- `min_ngram_length` — Optional. The minimum length of extracted ngram. The default and minimal value is 3. [`UInt*`](/docs/sql-reference/data-types/int-uint)
- `max_ngram_length` — Optional. The maximum length of extracted ngram. The default value is 100. Should be not less than `min_ngram_length`. [`UInt*`](/docs/sql-reference/data-types/int-uint)
- `min_cutoff_length` — Optional. If specified, only n-grams with length greater or equal than `min_cutoff_length` are returned. The default value is the same as `min_ngram_length`. Should be not less than `min_ngram_length` and not greater than `max_ngram_length`. [`UInt*`](/docs/sql-reference/data-types/int-uint)

**Returned value**

Returns an array of selected UTF-8 substrings CRC32 hashes. [`Array(UInt32)`](/docs/sql-reference/data-types/array)

**Examples**

**Usage example**

```
SELECT sparseGramsHashesUTF8('алиса', 3)
```

```
┌─sparseGramsHashesUTF8('алиса', 3)─┐
│ [4178533925,3855635300,561830861] │
└───────────────────────────────────┘
```

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

Introduced in: v25.5.0

Finds all substrings of a given UTF-8 string that have a length of at least `n`, where the hashes of the (n-1)-grams at the borders of the substring are strictly greater than those of any (n-1)-gram inside the substring.
Expects a UTF-8 string, throws an exception in case of an invalid UTF-8 sequence.
Uses `CRC32` as a hash function.

**Syntax**

```
sparseGramsUTF8(s[, min_ngram_length[, max_ngram_length[, min_cutoff_length]]])
```

**Arguments**