2020-04-03 13:23:32 +00:00
---
2022-08-28 14:53:34 +00:00
slug: /en/sql-reference/functions/string-replace-functions
2022-04-09 13:29:05 +00:00
sidebar_position: 42
2023-02-27 08:13:09 +00:00
sidebar_label: Replacing in Strings
2020-04-03 13:23:32 +00:00
---
2022-06-02 10:55:18 +00:00
# Functions for Searching and Replacing in Strings
2017-12-28 15:13:23 +00:00
2022-10-24 08:41:29 +00:00
:::note
2022-04-09 13:29:05 +00:00
Functions for [searching ](../../sql-reference/functions/string-search-functions.md ) and [other manipulations with strings ](../../sql-reference/functions/string-functions.md ) are described separately.
:::
2020-06-19 10:10:51 +00:00
2022-06-02 10:55:18 +00:00
## replaceOne(haystack, pattern, replacement)
2017-12-28 15:13:23 +00:00
2022-10-24 08:41:29 +00:00
Replaces the first occurrence of the substring ‘ pattern’ (if it exists) in ‘ haystack’ by the ‘ replacement’ string.
‘ pattern’ and ‘ replacement’ must be constants.
2017-12-28 15:13:23 +00:00
2022-06-02 10:55:18 +00:00
## replaceAll(haystack, pattern, replacement), replace(haystack, pattern, replacement)
2017-12-28 15:13:23 +00:00
2022-10-24 08:41:29 +00:00
Replaces all occurrences of the substring ‘ pattern’ in ‘ haystack’ by the ‘ replacement’ string.
2017-12-28 15:13:23 +00:00
2022-06-02 10:55:18 +00:00
## replaceRegexpOne(haystack, pattern, replacement)
2017-12-28 15:13:23 +00:00
2022-10-24 08:41:29 +00:00
Replaces the first occurrence of the substring matching the regular expression ‘ pattern’ in ‘ haystack‘ by the ‘ replacement‘ string.
‘ pattern‘ must be a constant [re2 regular expression ](https://github.com/google/re2/wiki/Syntax ).
‘ replacement’ must be a plain constant string or a constant string containing 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‘ string, escape it using `\` .
Also keep in mind that string literals require an extra escaping.
2017-12-28 15:13:23 +00:00
2022-10-24 08:41:29 +00:00
Example 1. Converting ISO dates to American format:
2017-12-28 15:13:23 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2017-12-28 15:13:23 +00:00
SELECT DISTINCT
EventDate,
replaceRegexpOne(toString(EventDate), '(\\d{4})-(\\d{2})-(\\d{2})', '\\2/\\3/\\1') AS res
FROM test.hits
LIMIT 7
FORMAT TabSeparated
```
2020-03-20 10:10:48 +00:00
``` text
2017-12-28 15:13:23 +00:00
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
```
Example 2. Copying a string ten times:
2020-03-20 10:10:48 +00:00
``` sql
2017-12-28 15:13:23 +00:00
SELECT replaceRegexpOne('Hello, World!', '.*', '\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0') AS res
```
2020-03-20 10:10:48 +00:00
``` text
2017-12-28 15:13:23 +00:00
┌─res────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World! │
└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
```
2022-06-02 10:55:18 +00:00
## replaceRegexpAll(haystack, pattern, replacement)
2017-12-28 15:13:23 +00:00
2022-10-24 08:41:29 +00:00
Like ‘ replaceRegexpOne‘ , but replaces all occurrences of the pattern. Example:
2017-12-28 15:13:23 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2017-12-28 15:13:23 +00:00
SELECT replaceRegexpAll('Hello, World!', '.', '\\0\\0') AS res
```
2020-03-20 10:10:48 +00:00
``` text
2017-12-28 15:13:23 +00:00
┌─res────────────────────────┐
│ HHeelllloo,, WWoorrlldd!! │
└────────────────────────────┘
```
As an exception, if a regular expression worked on an empty substring, the replacement is not made more than once.
Example:
2020-03-20 10:10:48 +00:00
``` sql
2017-12-28 15:13:23 +00:00
SELECT replaceRegexpAll('Hello, World!', '^', 'here: ') AS res
```
2020-03-20 10:10:48 +00:00
``` text
2017-12-28 15:13:23 +00:00
┌─res─────────────────┐
│ here: Hello, World! │
└─────────────────────┘
```
2018-04-23 06:20:21 +00:00
2022-06-02 10:55:18 +00:00
## regexpQuoteMeta(s)
2019-01-30 10:39:46 +00:00
The function adds a backslash before some predefined characters in the string.
2020-08-11 23:22:00 +00:00
Predefined characters: `\0` , `\\` , `|` , `(` , `)` , `^` , `$` , `.` , `[` , `]` , `?` , `*` , `+` , `{` , `:` , `-` .
2020-08-11 23:23:11 +00:00
This implementation slightly differs from re2::RE2::QuoteMeta. It escapes zero byte as `\0` instead of `\x00` and it escapes only required characters.
2019-01-30 10:39:46 +00:00
For more information, see the link: [RE2 ](https://github.com/google/re2/blob/master/re2/re2.cc#L473 )
2022-07-13 01:52:25 +00:00
## translate(s, from, to)
2022-07-24 23:15:48 +00:00
The function replaces characters in the string ‘ s’ in accordance with one-to-one character mapping defined by ‘ from’ and ‘ to’ strings. ‘ from’ and ‘ to’ must be constant ASCII strings of the same size. Non-ASCII characters in the original string are not modified.
2022-07-13 01:52:25 +00:00
Example:
``` sql
SELECT translate('Hello, World!', 'delor', 'DELOR') AS res
```
``` text
┌─res───────────┐
│ HELLO, WORLD! │
└───────────────┘
```
## translateUTF8(string, from, to)
2022-07-24 23:15:48 +00:00
Similar to previous function, but works with UTF-8 arguments. ‘ from’ and ‘ to’ must be valid constant UTF-8 strings of the same size.
2022-07-13 01:52:25 +00:00
Example:
``` sql
SELECT translateUTF8('Hélló, Wórld¡', 'óé¡', 'oe!') AS res
```
``` text
┌─res───────────┐
│ Hello, World! │
└───────────────┘
```