content/shared/sql-reference/functions/string.md
The {{< product-name >}} SQL implementation supports the following string functions for operating on string values:
Returns the ASCII value of the first character in a string.
[!Note]
asciireturns a 32-bit integer. To use with InfluxDB, cast the return value to 64-bit integer.
ascii(str)
{{< expand-wrapper >}}
{{% expand "View ascii query example" %}}
The following example uses the {{< influxdb3/home-sample-link >}}.
SELECT DISTINCT
room,
ascii(room)::BIGINT AS ascii
FROM home
| room | ascii |
|---|---|
| Kitchen | 75 |
| Living Room | 76 |
{{% /expand %}} {{< /expand-wrapper >}}
Returns the bit length of a string.
[!Note]
bit_lengthreturns a 32-bit integer. To use with InfluxDB, cast the return value to 64-bit integer.
bit_length(str)
{{< expand-wrapper >}}
{{% expand "View bit_length query example" %}}
The following example uses the {{< influxdb3/home-sample-link >}}.
SELECT DISTINCT
room,
bit_length(room)::BIGINT AS bit_length
FROM home
| room | bit_length |
|---|---|
| Living Room | 88 |
| Kitchen | 56 |
{{% /expand %}} {{< /expand-wrapper >}}
Trims the specified trim string from the start and end of a string. If no trim string is provided, all whitespace is removed from the start and end of the input string.
btrim(str[, trim_str])
{{< expand-wrapper >}}
{{% expand "View btrim query example" %}}
The following example uses the {{< influxdb3/home-sample-link >}}.
SELECT DISTINCT
room,
btrim(room::STRING, ' Room') AS btrim
FROM home
| room | btrim |
|---|---|
| Living Room | Living |
| Kitchen | Kitchen |
{{% /expand %}} {{< /expand-wrapper >}}
Alias of length.
Alias of length.
Returns the character with the specified ASCII or Unicode code value.
chr(expression)
{{< expand-wrapper >}}
{{% expand "View chr query example" %}}
SELECT
ascii,
chr(ascii) AS chr
FROM
(values (112),
(75),
(214)
) data(ascii)
| ascii | chr |
|---|---|
| 112 | p |
| 75 | K |
| 214 | Ö |
{{% /expand %}} {{< /expand-wrapper >}}
Concatenates multiple strings together.
concat(str[, ..., str_n])
{{< expand-wrapper >}}
{{% expand "View concat query example" %}}
The following example uses the {{< influxdb3/home-sample-link >}}.
SELECT
concat('At ', time::STRING, ', the ', room, ' was ', temp::STRING, '°C.') AS concat
FROM home
LIMIT 3
{{% influxdb/custom-timestamps %}}
| concat |
|---|
| At 2022-01-01T08:00:00, the Kitchen was 21.0°C. |
| At 2022-01-01T09:00:00, the Kitchen was 23.0°C. |
| At 2022-01-01T10:00:00, the Kitchen was 22.7°C. |
{{% /influxdb/custom-timestamps %}}
{{% /expand %}} {{< /expand-wrapper >}}
Concatenates multiple strings together with a specified separator.
concat_ws(separator, str[, ..., str_n])
{{< expand-wrapper >}}
{{% expand "View concat_ws query example" %}}
The following example uses the {{< influxdb3/home-sample-link >}}.
SELECT
concat_ws(' -- ', time::STRING, room, temp::STRING) AS concat_ws
FROM home
LIMIT 3
{{% influxdb/custom-timestamps %}}
| concat_ws |
|---|
| 2022-01-01T08:00:00 -- Kitchen -- 21.0 |
| 2022-01-01T09:00:00 -- Kitchen -- 23.0 |
| 2022-01-01T10:00:00 -- Kitchen -- 22.7 |
{{% /influxdb/custom-timestamps %}}
{{% /expand %}} {{< /expand-wrapper >}}
Returns true if a string contains a search string (case-sensitive).
contains(str, search_str)
Tests if a string ends with a substring.
ends_with(str, substr)
{{< expand-wrapper >}}
{{% expand "View ends_with query example" %}}
SELECT
string,
ends_with(string, 'USA') AS ends_with
FROM
(values ('New York, USA'),
('London, UK'),
('San Francisco, USA')
) data(string)
| string | ends_with |
|---|---|
| New York, USA | true |
| London, UK | false |
| San Francisco, USA | true |
{{% /expand %}} {{< /expand-wrapper >}}
Returns the position of a string in a comma-delimited list of substrings. Returns 0 if the string is not in the list of substrings.
find_in_set(str, strlist)
strlist.{{< expand-wrapper >}}
{{% expand "View find_in_set query example" %}}
SELECT
string,
find_in_set(string, 'Isaac,John,Sara') AS find_in_set
FROM
(values ('John'),
('Sarah'),
('Isaac')
) data(string)
| string | find_in_set |
|---|---|
| John | 2 |
| Sarah | 0 |
| Isaac | 1 |
{{% /expand %}} {{< /expand-wrapper >}}
Capitalizes the first character in each word in the input string. Words are delimited by non-alphanumeric characters.
initcap(str)
{{< expand-wrapper >}}
{{% expand "View initcap query example" %}}
SELECT
string,
initcap(string) AS initcap
FROM
(values ('hello world'),
('hello-world'),
('hello_world')
) data(string)
| string | initcap |
|---|---|
| hello world | Hello World |
| hello-world | Hello-World |
| hello_world | Hello_World |
{{% /expand %}} {{< /expand-wrapper >}}
Returns the location where a substring first appears in a string (starting at 1). If the substring is not in the string, the function returns 0.
instr(str, substr)
{{< expand-wrapper >}}
{{% expand "View instr query example" %}}
SELECT
string,
instr(string, 'neighbor') AS instr
FROM
(values ('good neighbor'),
('bad neighbor'),
('next-door neighbor'),
('friend')
) data(string)
| string | instr |
|---|---|
| good neighbor | 6 |
| bad neighbor | 5 |
| next-door neighbor | 11 |
| friend | 0 |
{{% /expand %}} {{< /expand-wrapper >}}
Returns a specified number of characters from the left side of a string.
left(str, n)
{{< expand-wrapper >}}
{{% expand "View left query example" %}}
The following example uses the {{< influxdb3/home-sample-link >}}.
SELECT DISTINCT
room,
left(room::STRING, 3) AS left
FROM home
| room | left |
|---|---|
| Kitchen | Kit |
| Living Room | Liv |
{{% /expand %}} {{< /expand-wrapper >}}
Returns the number of characters in a string.
[!Note]
char_lengthreturns a 32-bit integer. To use with InfluxDB, cast the return value to 64-bit integer.
length(str)
{{< expand-wrapper >}}
{{% expand "View length query example" %}}
The following example uses the {{< influxdb3/home-sample-link >}}.
SELECT DISTINCT
room,
length(room)::BIGINT AS length
FROM home
| room | length |
|---|---|
| Kitchen | 7 |
| Living Room | 11 |
{{% /expand %}} {{< /expand-wrapper >}}
Returns the Levenshtein distance between two strings.
levenshtein(str1, str2)
{{< expand-wrapper >}}
{{% expand "View levenshtein query example" %}}
SELECT
string1,
string2,
levenshtein(string1, string2) AS levenshtein
FROM
(values ('kitten', 'sitting'),
('puppy', 'jumping'),
('cow', 'lowing')
) data(string1, string2)
| string1 | string2 | levenshtein |
|---|---|---|
| kitten | sitting | 3 |
| puppy | jumping | 5 |
| cow | lowing | 4 |
{{% /expand %}} {{< /expand-wrapper >}}
Converts a string to lower-case.
lower(str)
{{< expand-wrapper >}}
{{% expand "View lower query example" %}}
The following example uses the {{< influxdb3/home-sample-link >}}.
SELECT DISTINCT
room,
lower(room::STRING) AS lower
FROM home
| room | lower |
|---|---|
| Kitchen | kitchen |
| Living Room | living room |
{{% /expand %}} {{< /expand-wrapper >}}
Pads the left side of a string with another string to a specified string length.
lpad(str, n[, padding_str])
{{< expand-wrapper >}}
{{% expand "View lpad query example" %}}
The following example uses the {{< influxdb3/home-sample-link >}}.
SELECT DISTINCT
room,
lpad(room::STRING, 14, '-') AS lpad
FROM home
| room | lpad |
|---|---|
| Kitchen | -------Kitchen |
| Living Room | ---Living Room |
{{% /expand %}} {{< /expand-wrapper >}}
Removes leading spaces from a string.
ltrim(str)
{{< expand-wrapper >}}
{{% expand "View ltrim query example" %}}
SELECT
string,
ltrim(string) AS ltrim
FROM
(values (' Leading spaces'),
('Trailing spaces '),
(' Leading and trailing spaces ')
) data(string)
| string | ltrim |
|---|---|
| Leading spaces | Leading spaces |
| Trailing spaces | Trailing spaces |
| Leading and trailing spaces | Leading and trailing spaces |
{{% /expand %}} {{< /expand-wrapper >}}
Returns the length of a string in bytes.
[!Note]
lengthreturns a 32-bit integer. To use with InfluxDB, cast the return value to 64-bit integer.
octet_length(str)
{{< expand-wrapper >}}
{{% expand "View octet_length query example" %}}
The following example uses the {{< influxdb3/home-sample-link >}}.
SELECT DISTINCT
room,
octet_length(room)::BIGINT AS octet_length
FROM home
| room | octet_length |
|---|---|
| Living Room | 11 |
| Kitchen | 7 |
{{% /expand %}} {{< /expand-wrapper >}}
Replaces part of a string with another substring using a specified starting position and number of characters to replace.
overlay(str PLACING substr FROM pos [FOR count])
str).
Can be a constant, column, or function, and any combination of string operators.substr).str) to replace with the
substring (substr) beginning from the start position (pos).
If not specified, the function uses the length of the substring.{{< expand-wrapper >}}
{{% expand "View overlay query example" %}}
SELECT
string,
overlay(string PLACING '****' FROM 1 FOR 12) AS overlay
FROM
(values ('2223000048410010'),
('2222420000001113'),
('4917484589897107')
) data(string)
| string | overlay |
|---|---|
| 2223000048410010 | ****0010 |
| 2222420000001113 | ****1113 |
| 4917484589897107 | ****7107 |
{{% /expand %}} {{< /expand-wrapper >}}
Returns the position of a substring in a string.
position(substr IN str)
{{< expand-wrapper >}}
{{% expand "View position query example" %}}
SELECT
string,
position('oo' IN string) AS position
FROM
(values ('cool'),
('scoop'),
('ice cream')
) data(string)
| string | position |
|---|---|
| cool | 2 |
| scoop | 3 |
| ice cream | 0 |
{{% /expand %}} {{< /expand-wrapper >}}
Returns a string with an input string repeated a specified number of times.
repeat(str, n)
{{< expand-wrapper >}}
{{% expand "View repeat query example" %}}
SELECT
string,
repeat(string, 3) AS repeat
FROM
(values ('foo '),
('bar '),
('baz ')
) data(string)
| string | repeat |
|---|---|
| foo | foo foo foo |
| bar | bar bar bar |
| baz | baz baz baz |
{{% /expand %}} {{< /expand-wrapper >}}
Replaces all occurrences of a specified substring in a string with a new substring.
replace(str, substr, replacement)
{{< expand-wrapper >}}
{{% expand "View replace query example" %}}
The following example uses the {{< influxdb3/home-sample-link >}}.
SELECT DISTINCT
room,
replace(room::STRING, ' ', '_') AS replace
FROM home
| room | replace |
|---|---|
| Kitchen | Kitchen |
| Living Room | Living_Room |
{{% /expand %}} {{< /expand-wrapper >}}
Reverses the character order of a string.
reverse(str)
{{< expand-wrapper >}}
{{% expand "View reverse query example" %}}
The following example uses the {{< influxdb3/home-sample-link >}}.
SELECT DISTINCT
room,
reverse(room::STRING) AS reverse
FROM home
| room | reverse |
|---|---|
| Kitchen | nehctiK |
| Living Room | mooR gniviL |
{{% /expand %}} {{< /expand-wrapper >}}
Returns a specified number of characters from the right side of a string.
right(str, n)
{{< expand-wrapper >}}
{{% expand "View right query example" %}}
The following example uses the {{< influxdb3/home-sample-link >}}.
SELECT DISTINCT
room,
right(room::STRING, 3) AS right
FROM home
| room | right |
|---|---|
| Living Room | oom |
| Kitchen | hen |
{{% /expand %}} {{< /expand-wrapper >}}
Pads the right side of a string with another string to a specified string length.
rpad(str, n[, padding_str])
{{< expand-wrapper >}}
{{% expand "View rpad query example" %}}
The following example uses the {{< influxdb3/home-sample-link >}}.
SELECT DISTINCT
room,
rpad(room::STRING, 14, '-') AS rpad
FROM home
| room | rpad |
|---|---|
| Kitchen | Kitchen------- |
| Living Room | Living Room--- |
{{% /expand %}} {{< /expand-wrapper >}}
Removes trailing spaces from a string.
rtrim(str)
{{< expand-wrapper >}}
{{% expand "View rtrim query example" %}}
SELECT
string,
rtrim(string) AS rtrim
FROM
(values (' Leading spaces'),
('Trailing spaces '),
(' Leading and trailing spaces ')
) data(string)
| string | rtrim |
|---|---|
| Leading spaces | Leading spaces |
| Trailing spaces | Trailing spaces |
| Leading and trailing spaces | Leading and trailing spaces |
{{% /expand %}} {{< /expand-wrapper >}}
Splits a string based on a specified delimiter and returns the substring in the specified position.
split_part(str, delimiter, pos)
{{< expand-wrapper >}}
{{% expand "View split_part query example" %}}
SELECT
url,
split_part(url, '.', 1) AS split_part
FROM
(values ('www.influxdata.com'),
('docs.influxdata.com'),
('community.influxdata.com')
) data(url)
| url | split_part |
|---|---|
| www.influxdata.com | www |
| docs.influxdata.com | docs |
| community.influxdata.com | community |
{{% /expand %}} {{< /expand-wrapper >}}
Tests if a string starts with a substring.
starts_with(str, substr)
{{< expand-wrapper >}}
{{% expand "View starts_with query example" %}}
The following example uses the {{< influxdb3/home-sample-link >}}.
SELECT DISTINCT
room,
starts_with(room::STRING, 'Kit') AS starts_with
FROM home
| room | starts_with |
|---|---|
| Kitchen | true |
| Living Room | false |
{{% /expand %}} {{< /expand-wrapper >}}
Returns the starting position of a specified substring in a string. Positions begin at 1. If the substring does not exist in the string, the function returns 0.
[!Note]
strposreturns a 32-bit integer. To use with InfluxDB, cast the return value to 64-bit integer.
strpos(str, substr)
{{< expand-wrapper >}}
{{% expand "View strpos query example" %}}
The following example uses the {{< influxdb3/home-sample-link >}}.
SELECT DISTINCT
room,
strpos(room::STRING, 'Room')::BIGINT AS strpos
FROM home
| room | strpos |
|---|---|
| Kitchen | 0 |
| Living Room | 8 |
{{% /expand %}} {{< /expand-wrapper >}}
Extracts a substring of a specified number of characters from a specific starting position in a string.
substr(str, start_pos[, length])
{{< expand-wrapper >}}
{{% expand "View substr query example" %}}
The following example uses the {{< influxdb3/home-sample-link >}}.
SELECT DISTINCT
room,
substr(room::STRING, 3, 5) AS substr
FROM home
| room | substr |
|---|---|
| Living Room | ving |
| Kitchen | tchen |
{{% /expand %}} {{< /expand-wrapper >}}
Returns the substring that occurs before or after the specified number (count)
of delimiter (delimiter) occurrences in a string (str).
If the count is positive, the function returns everything to the left of the
final delimiter (counting from the left).
If the count is negative, the function returns everything to the right of the
final delimiter (counting from the right).
substr_index(str, delimiter, count)
str).
Can be a constant, column, or function, and any combination of string operators.delimiter) to split on.
Can be a constant, column, or function, and any combination of arithmetic operators.
Supports positive and negative numbers.{{< expand-wrapper >}}
{{% expand "View substr_index query example" %}}
SELECT
url,
substr_index(url, '.', 1) AS subdomain,
substr_index(url, '.', -1) AS tld
FROM
(values ('docs.influxdata.com'),
('community.influxdata.com'),
('cloud2.influxdata.com')
) data(url)
| url | subdomain | tld |
|---|---|---|
| docs.influxdata.com | docs | com |
| community.influxdata.com | community | com |
| arrow.apache.org | arrow | org |
{{% /expand %}} {{< /expand-wrapper >}}
Alias of substr.
Alias of substr_index.
Translates characters in a string to specified translation characters.
translate(str, chars, translation)
{{< expand-wrapper >}}
{{% expand "View translate query example" %}}
The following example uses the {{< influxdb3/home-sample-link >}}.
SELECT DISTINCT
room,
translate(room::STRING, 'Rom', 'sOn') AS translate
FROM home
| room | translate |
|---|---|
| Living Room | Living sOOn |
| Kitchen | Kitchen |
{{% /expand %}} {{< /expand-wrapper >}}
Converts an integer to a hexadecimal string.
to_hex(int)
{{< expand-wrapper >}}
{{% expand "View to_hex query example" %}}
SELECT
int,
to_hex(int) AS to_hex
FROM
(values (123),
(345),
(678)
) data(int)
| int | to_hex |
|---|---|
| 123 | 7b |
| 345 | 159 |
| 678 | 2a6 |
{{% /expand %}} {{< /expand-wrapper >}}
Removes leading and trailing spaces from a string.
trim(str)
{{< expand-wrapper >}}
{{% expand "View trim query example" %}}
SELECT
string,
trim(string) AS trim
FROM
(values (' Leading spaces'),
('Trailing spaces '),
(' Leading and trailing spaces ')
) data(string)
| string | trim |
|---|---|
| Leading spaces | Leading spaces |
| Trailing spaces | Trailing spaces |
| Leading and trailing spaces | Leading and trailing spaces |
{{% /expand %}} {{< /expand-wrapper >}}
Converts a string to upper-case.
upper(str)
{{< expand-wrapper >}}
{{% expand "View upper query example" %}}
The following example uses the {{< influxdb3/home-sample-link >}}.
SELECT DISTINCT
room,
upper(room::STRING) AS upper
FROM home
| room | upper |
|---|---|
| Living Room | LIVING ROOM |
| Kitchen | KITCHEN |
{{% /expand %}} {{< /expand-wrapper >}}
Returns a UUID v4 string value that is unique per row.
uuid()
{{< expand-wrapper >}}
{{% expand "View uuid query example" %}}
The following example uses the sample data set provided in the {{< influxdb3/home-sample-link >}}.
SELECT
room,
uuid() AS uuid
FROM (SELECT DISTINCT room FROM home)
| room | uuid |
|---|---|
| Kitchen | f0b41da9-e334-4b7d-b925-a54ca6b082f3 |
| Living Room | c31be90e-c4ed-4304-b633-47b969ef3ab6 |
{{% /expand %}} {{< /expand-wrapper >}}