Functions for string replacement

**Returned value**

Returns a string with all occurrences of pattern replaced. [`String`](/docs/sql-reference/data-types/string)

**Examples**

**Replace all occurrences**

```
SELECT replaceAll('Hello, Hello world', 'Hello', 'Hi') AS res;
```

```
┌─res──────────┐
│ Hi, Hi world │
└──────────────┘
```

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

Introduced in: v1.1.0

Replaces the first occurrence of the substring `pattern` in `haystack` by the `replacement` string.

**Syntax**

```
replaceOne(haystack, pattern, replacement)
```

**Arguments**

- `haystack` — The input string to search in. [`String`](/docs/sql-reference/data-types/string)
- `pattern` — The substring to find and replace. [`const String`](/docs/sql-reference/data-types/string)
- `replacement` — The string to replace the pattern with. [`const String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns a string with the first occurrence of pattern replaced. [`String`](/docs/sql-reference/data-types/string)

**Examples**

**Replace first occurrence**

```
SELECT replaceOne('Hello, Hello world', 'Hello', 'Hi') AS res;
```

```
┌─res─────────────┐
│ Hi, Hello world │
└─────────────────┘
```

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

Introduced in: v1.1.0

Like `replaceRegexpOne` but replaces all occurrences of the pattern.
As an exception, if a regular expression worked on an empty substring, the replacement is not made more than once.

**Syntax**

```
replaceRegexpAll(haystack, pattern, replacement)
```

**Aliases**: `REGEXP_REPLACE`

**Arguments**

- `haystack` — The input string to search in. [`String`](/docs/sql-reference/data-types/string)
- `pattern` — The regular expression pattern to find. [`const String`](/docs/sql-reference/data-types/string)
- `replacement` — The string to replace the pattern with, may contain substitutions. [`const String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns a string with all regex matches replaced. [`String`](/docs/sql-reference/data-types/string)

**Examples**

**Replace all characters with doubled version**

```
SELECT replaceRegexpAll('Hello123', '.', '\\\\0\\\\0') AS res
```

```
┌─res──────────────────┐
│ HHeelllloo112233     │
└──────────────────────┘
```

**Empty substring replacement example**

```
SELECT replaceRegexpAll('Hello, World!', '^', 'here: ') AS res
```

```
┌─res─────────────────┐
│ here: Hello, World! │
└─────────────────────┘
```

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

Introduced in: v1.1.0

Replaces the first occurrence of the substring matching the regular expression `pattern` (in re2 syntax) in `haystack` by the `replacement` string.
`replacement` can contain substitutions `\0-\9`.
Substitutions `\1-\9` correspond to the 1st to 9th capturing group (submatch), substitution `\0` corresponds to the entire match.
To use a verbatim `\` character in the `pattern` or `replacement` strings, escape it using `\`.
Also keep in mind that string literals require extra escaping.

**Syntax**

```
replaceRegexpOne(haystack, pattern, replacement)
```

**Arguments**

- `haystack` — The input string to search in. [`String`](/docs/sql-reference/data-types/string)
- `pattern` — The regular expression pattern to find. [`const String`](/docs/sql-reference/data-types/string)
- `replacement` — The string to replace the pattern with, may contain substitutions. [`const String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns a string with the first regex match replaced. [`String`](/docs/sql-reference/data-types/string)

**Examples**

**Converting ISO dates to American format**

```
SELECT DISTINCT
    EventDate,
    replaceRegexpOne(toString(EventDate), '(\\d{4})-(\\d{2})-(\\d{2})', '\\2/\\3/\\1') AS res
FROM test.hits
LIMIT 7
FORMAT TabSeparated
```

```
2014-03-17      03/17/2014
2014-03-18      03/18/2014
2014-03-19      03/19/2014
2014-03-20      03/20/2014
2014-03-21      03/21/2014
2014-03-22      03/22/2014
2014-03-23      03/23/2014
```

**Copying a string ten times**

```
SELECT replaceRegexpOne('Hello, World!', '.*', '\\\\0\\\\0\\\\0\\\\0\\\\0\\\\0\\\\0\\\\0\\\\0\\\\0') AS res
```

```
┌─res────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World! │
└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
```

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

Introduced in: v22.7.0

Replaces characters in the string `s` using a one-to-one character mapping defined by `from` and `to` strings.
`from` and `to` must be constant ASCII strings.
If `from` and `to` have equal sizes, each occurrence of the first character of `first` in `s` is replaced by the first character of `to`, the second character of `first` in `s` is replaced by the second character of `to`, etc.
If `from` contains more characters than `to`, all occurrences of the characters at the end of `from` that have no corresponding character in `to` are deleted from `s`.
Non-ASCII characters in `s` are not modified by the function.

**Syntax**

```
translate(s, from, to)
```

**Arguments**

- `s` — The input string to translate. [`String`](/docs/sql-reference/data-types/string)
- `from` — A constant ASCII string containing characters to replace. [`const String`](/docs/sql-reference/data-types/string)
- `to` — A constant ASCII string containing replacement characters. [`const String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns a string with character translations applied. [`String`](/docs/sql-reference/data-types/string)

**Examples**

**Character mapping**

```
SELECT translate('Hello, World!', 'delor', 'DELOR') AS res
```

```
┌─res───────────┐
│ HELLO, WORLD! │
└───────────────┘
```

**Different lengths**

```
SELECT translate('clickhouse', 'clickhouse', 'CLICK') AS res
```

```
┌─res───┐
│ CLICK │
└───────┘
```

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

Introduced in: v22.7.0

Like [`translate`](#translate) but assumes `s`, `from` and `to` are UTF-8 encoded strings.

**Syntax**

```
translateUTF8(s, from, to)
```

**Arguments**

- `s` — UTF-8 input string to translate. [`String`](/docs/sql-reference/data-types/string)
- `from` — A constant UTF-8 string containing characters to replace. [`const String`](/docs/sql-reference/data-types/string)
- `to` — A constant UTF-8 string containing replacement characters. [`const String`](/docs/sql-reference/data-types/string)

**Returned value**

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

**Examples**

**UTF-8 character translation**

```
SELECT translateUTF8('Münchener Straße', 'üß', 'us') AS res;
```

```
┌─res──────────────┐
│ Munchener Strase │
└──────────────────┘
```

- [format](#format)- [overlay](#overlay)- [overlayUTF8](#overlayUTF8)- [printf](#printf)- [regexpQuoteMeta](#regexpQuoteMeta)- [replaceAll](#replaceAll)- [replaceOne](#replaceOne)- [replaceRegexpAll](#replaceRegexpAll)- [replaceRegexpOne](#replaceRegexpOne)- [translate](#translate)- [translateUTF8](#translateUTF8)

Was this page helpful?