Functions for working with strings

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

Introduced in: v24.1.0

Returns the [Punycode](https://en.wikipedia.org/wiki/Punycode) representation of a string.
The string must be UTF8-encoded, otherwise the behavior is undefined.

**Syntax**

```
punycodeEncode(s)
```

**Arguments**

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

**Returned value**

Returns a Punycode representation of the input value. [`String`](/docs/sql-reference/data-types/string)

**Examples**

**Usage example**

```
SELECT punycodeEncode('München')
```

```
┌─punycodeEncode('München')─┐
│ Mnchen-3ya                │
└───────────────────────────┘
```

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

Introduced in: v23.2.0

Extracts the first string in `haystack` that matches the regexp pattern and corresponds to the regex group index.

**Syntax**

```
regexpExtract(haystack, pattern[, index])
```

**Aliases**: `REGEXP_EXTRACT`

**Arguments**

- `haystack` — String, in which regexp pattern will be matched. [`String`](/docs/sql-reference/data-types/string)
- `pattern` — String, regexp expression. `pattern` may contain multiple regexp groups, `index` indicates which regex group to extract. An index of 0 means matching the entire regular expression. [`const String`](/docs/sql-reference/data-types/string)
- `index` — Optional. An integer number greater or equal 0 with default 1. It represents which regex group to extract. [`(U)Int*`](/docs/sql-reference/data-types/int-uint)

**Returned value**

Returns a string match [`String`](/docs/sql-reference/data-types/string)

**Examples**

**Usage example**

```
SELECT
    regexpExtract('100-200', '(\\d+)-(\\d+)', 1),
    regexpExtract('100-200', '(\\d+)-(\\d+)', 2),
    regexpExtract('100-200', '(\\d+)-(\\d+)', 0),
    regexpExtract('100-200', '(\\d+)-(\\d+)');
```

```
┌─regexpExtract('100-200', '(\\d+)-(\\d+)', 1)─┬─regexpExtract('100-200', '(\\d+)-(\\d+)', 2)─┬─regexpExtract('100-200', '(\\d+)-(\\d+)', 0)─┬─regexpExtract('100-200', '(\\d+)-(\\d+)')─┐
│ 100                                          │ 200                                          │ 100-200                                      │ 100                                       │
└──────────────────────────────────────────────┴──────────────────────────────────────────────┴──────────────────────────────────────────────┴───────────────────────────────────────────┘
```

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

Introduced in: v26.3.0

Removes diacritical marks (accents) from a UTF-8 string by decomposing characters via NFD,
stripping combining marks (Unicode category Mn), then recomposing via NFC.

**Syntax**

```
removeDiacriticsUTF8(str)
```

**Aliases**: `removeAccentsUTF8`

**Arguments**

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

**Returned value**

UTF-8 string with diacritics removed. [`String`](/docs/sql-reference/data-types/string)

**Examples**

**Basic accent removal**

```
SELECT removeDiacriticsUTF8('café résumé naïve')
```

```
┌─removeDiacriticsUTF8('café résumé naïve')─┐
│ cafe resume naive                          │
└────────────────────────────────────────────┘
```

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

Introduced in: v20.1.0

Concatenates a string as many times with itself as specified.

**Syntax**

```
repeat(s, n)
```

**Arguments**

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

**Returned value**

A string containing string `s` repeated `n` times. If `n` is negative, the function returns the empty string. [`String`](/docs/sql-reference/data-types/string)

**Examples**

**Usage example**

```
SELECT repeat('abc', 10)
```

```
┌─repeat('abc', 10)──────────────┐
│ abcabcabcabcabcabcabcabcabcabc │
└────────────────────────────────┘
```

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

Introduced in: v1.1.0

Reverses a sequence of Unicode code points in a string.
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**

```
reverseUTF8(s)
```

**Arguments**

- `s` — String containing valid UTF-8 encoded text. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns a string with the sequence of Unicode code points reversed. [`String`](/docs/sql-reference/data-types/string)

**Examples**

**Usage example**

```
SELECT reverseUTF8('ClickHouse')
```

```
esuoHkcilC
```

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

Introduced in: v22.1.0

Returns a substring of string `s` with a specified `offset` starting from the right.

**Syntax**

```
right(s, offset)
```

**Arguments**

- `s` — The 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 right('Hello', 3)
```

```
llo
```

**Negative offset**

```
SELECT right('Hello', -3)
```

```
lo
```

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

Introduced in: v21.8.0

Pads a string from the right with spaces or with a specified string (multiple times, if needed) until the resulting string reaches the specified `length`.

**Syntax**

```
rightPad(string, length[, pad_string])
```

**Aliases**: `rpad`

**Arguments**

- `string` — Input string that should be padded. [`String`](/docs/sql-reference/data-types/string)
- `length` — The length of the resulting string. If the value is smaller than the input string length, then the input string is shortened to `length` characters. [`(U)Int*`](/docs/sql-reference/data-types/int-uint)
- `pad_string` — Optional. The string to pad the input string with. If not specified, then the input string is padded with spaces. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

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

**Examples**

**Usage example**

```
SELECT rightPad('abc', 7, '*'), rightPad('abc', 7)
```

```
┌─rightPad('abc', 7, '*')─┬─rightPad('abc', 7)─┐
│ abc****                 │ abc                │
└─────────────────────────┴────────────────────┘
```

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

Introduced in: v21.8.0

Pads the string from the right with spaces or a specified string (multiple times, if needed) until the resulting string reaches the given length.
Unlike [`rightPad`](#rightPad) which measures the string length in bytes, the string length is measured in code points.

**Syntax**

```
rightPadUTF8(string, length[, pad_string])
```

**Arguments**

- `string` — Input string that should be padded. [`String`](/docs/sql-reference/data-types/string)
- `length` — The length of the resulting string. If the value is smaller than the input string length, then the input string is shortened to `length` characters. [`(U)Int*`](/docs/sql-reference/data-types/int-uint)
- `pad_string` — Optional. The string to pad the input string with. If not specified, then the input string is padded with spaces. [`String`](/docs/sql-reference/data-types/string)

**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)─┐
│ абвг***                      │ абвг                    │
└──────────────────────────────┴─────────────────────────┘
```