String Functions in Confluent Cloud for Apache Flink¶
Confluent Cloud for Apache Flink® provides these built-in string functions to use in SQL queries:
ASCII¶
Gets the ASCII value of the first character of a string.
- Syntax
ASCII(string)
- Description
- The
ASCII
function returns the numeric value of the first character of the specified string. Returns NULL ifstring
is NULL. - Examples
-- returns 97 SELECT ASCII('abc'); -- returns NULL SELECT ASCII(CAST(NULL AS VARCHAR));
string1 || string2¶
Concatenates two strings.
CHARACTER_LENGTH¶
Gets the length of a string.
- Syntax
CHARACTER_LENGTH(string)
- Description
The
CHARACTER_LENGTH
function returns the number of characters in the specified string.This function can be abbreviated to
CHAR_LENGTH(string)
.- Examples
-- returns 18 SELECT CHAR_LENGTH('Thomas A. Anderson');
CHR¶
Gets the character for an ASCII code.
- Syntax
CHR(integer)
- Description
The
CHR
function returns the ASCII character that has the binary equivalent to the specified integer. Returns NULL ifinteger
is NULL.If
integer
is larger than 255, the function computes the modulus ofinteger
divided by 255 first and returnsCHR
of the modulus.- Examples
-- returns 'a' SELECT CHR(97); -- returns 'a' SELECT CHR(353);
CONCAT¶
Concatenates a list of strings.
- Syntax
CONCAT(string1, string2, ...)
- Description
- The
CONCAT
function returns the concatenation of the specified strings. Returns NULL if any argument is NULL. - Example
-- returns "AABBCC" SELECT CONCAT('AA', 'BB', 'CC');
- Related functions
CONCAT_WS¶
Concatenates a list of strings with a separator.
- Syntax
CONCAT_WS(string1, string2, string3, ...)
- Description
The
CONCAT_WS
function returns a string that concatenatesstring2, string3, ...
with the separator specified bystring1
.The separator is added between the strings to be concatenated.
Returns NULL If
string1
is NULL.
- Example
-- returns "AA~BB~~CC" SELECT CONCAT_WS('~', 'AA', 'BB', '', 'CC');
- Related functions
DECODE¶
Decodes a binary into a string.
- Syntax
DECODE(binary, string)
- Description
The
DECODE
function decodes the binary argument into a string using the specified character set. Returns NULL if either argument is null.These are the supported character set strings:
- ‘ISO-8859-1’
- ‘US-ASCII’
- ‘UTF-8’
- ‘UTF-16BE’
- ‘UTF-16LE’
- ‘UTF-16’
- Related function
ENCODE¶
Encodes a string to a BINARY.
- Syntax
ENCODE(string1, string2)
- Description
The
ENCODE
function encodesstring1
into a BINARY using the specifiedstring2
character set. Returns NULL if either argument is null.These are the supported character set strings:
- ‘ISO-8859-1’
- ‘US-ASCII’
- ‘UTF-8’
- ‘UTF-16BE’
- ‘UTF-16LE’
- ‘UTF-16’
- Related function
FROM_BASE64¶
Decodes a base-64 encoded string.
- Syntax
FROM_BASE64(string)
- Description
- The
FROM_BASE64
function returns the base64-decoded result from the specified string. Returns NULL ifstring
is NULL. - Example
-- returns "hello world" SELECT FROM_BASE64('aGVsbG8gd29ybGQ=');
- Related function
INITCAP¶
Titlecase a string.
- Syntax
INITCAP(string)
- Description
The
INITCAP
function returns a string that has the first character of each word converted to uppercase and the other characters converted to lowercase.A “word” is assumed to be a sequence of alphanumeric characters.
- Example
-- returns "Title Case This String" SELECT INITCAP('title case this string');
- Related functions
INSTR¶
Find a substring in a string.
- Syntax
INSTR(string1, string2)
- Description
The
INSTR
function returns the position of the first occurrence ofstring2
instring1
. Returns NULL if either argument is NULL.The search is case-sensitive.
- Example
-- returns 33 SELECT INSTR('The quick brown fox jumped over the lazy dog.', 'the');
- Related function
LEFT¶
Gets the leftmost characters in a string.
- Syntax
LEFT(string, integer)
- Description
- The
LEFT
function returns the leftmostinteger
characters from the specified string. Returns an empty string ifinteger
is negative. Returns NULL if either argument is NULL. - Example
-- returns "Morph" SELECT LEFT('Morpheus', 5);
- Related function
LOCATE¶
Finds a substring in a string after a specified position.
- Syntax
LOCATE(string1, string2[, integer])
- Description
- The
LOCATE
function returns the position of the first occurrence ofstring1
instring2
after positioninteger
. Returns 0 ifstring1
isn’t found. Returns NULL if any of the arguments is NULL. - Example
-- returns 12 SELECT LOCATE('the', 'the play’s the thing', 10);
LOWER¶
Lowercases a string.
- Syntax
LOWER(string)
- Description
The
LOWER
function returns the specified string in lowercase.To uppercase a string, use the UPPER function.
- Example
-- returns "the quick brown fox jumped over the lazy dog." SELECT LOWER('The Quick Brown Fox Jumped Over The Lazy Dog.');
- Related functions
LPAD¶
Left-pad a string.
- Syntax
LPAD(string1, integer, string2)
- Description
The
LPAD
function returns a new string fromstring1
that’s left-padded withstring2
to a length ofinteger
characters.If the length of
string1
is shorter thaninteger
, theLPAD
function returnsstring1
shortened tointeger
characters.To right-pad a string, use the RPAD function.
- Examples
-- returns "??hi" SELECT LPAD('hi', 4, '??'); -- returns "h" SELECT LPAD('hi', 1, '??');
Related function - RPAD
LTRIM¶
Removes left whitespaces from a string.
- Syntax
LTRIM(string)
- Description
The
LTRIM
function removes the left whitespaces from the specified string.To remove the right whitespaces from a string, use the RTRIM function.
- Example
-- returns "This is a test string." SELECT LTRIM(' This is a test string.');
OVERLAY¶
Replaces characters in a string with another string.
- Syntax
OVERLAY(string1 PLACING string2 FROM integer1 [ FOR integer2 ])
- Description
The
OVERLAY
function returns a string that replacesinteger2
characters ofstring1
withstring2
, starting from positioninteger1
.If
integer2
isn’t specified, the default is the length ofstring2
.- Examples
-- returns "xxxxxxxxx" SELECT OVERLAY('xxxxxtest' PLACING 'xxxx' FROM 6); -- returns "xxxxxxxxxst" SELECT OVERLAY('xxxxxtest' PLACING 'xxxx' FROM 6 FOR 2);
PARSE_URL¶
Gets parts of a URL.
- Syntax
PARSE_URL(string1, string2[, string3])
- Description
The
PARSE_URL
function returns the part specified bystring2
from the URL instring1
.For a URL that has a query, the optional
string3
argument specifies the key to extract from the query string.Returns NULL if
string1
orstring2
is NULL.These are the valid values for
string2
:- ‘AUTHORITY’
- ‘FILE’
- ‘HOST’
- ‘PATH’
- ‘PROTOCOL’
- ‘QUERY’
- ‘REF’
- ‘USERINFO’
- Example
-- returns 'confluent.io' SELECT PARSE_URL('http://confluent.io/path1/p.php?k1=v1&k2=v2#Ref1', 'HOST'); -- returns 'v1' SELECT PARSE_URL('http://confluent.io/path1/p.php?k1=v1&k2=v2#Ref1', 'QUERY', 'k1');
POSITION¶
Finds a substring in a string.
- Syntax
POSITION(string1 IN string2)
- Description
The
POSITION
function returns the position of the first occurrence ofstring1
instring2
. Returns 0 ifstring1
isn’t found instring2
.The position is 1-based, so the index of the first character is 1.
- Examples
-- returns 1 SELECT POSITION('the' IN 'the quick brown fox'); -- returns 17 SELECT POSITION('fox' IN 'the quick brown fox');
REGEXP¶
Matches a string against a regular expression.
- Syntax
REGEXP(string1, string2)
- Description
- The
REGEXP
function returns TRUE if any (possibly empty) substring ofstring1
matches the regular expression instring2
; otherwise, FALSE. Returns NULL if either of the arguments is NULL. - Examples
-- returns TRUE SELECT REGEXP('800 439 3207', '.?(\d{3}).*(\d{3}).*(\d{4})'); -- returns TRUE SELECT REGEXP('2023-05-04', '((\d{4}.\d{2}).(\d{2}))');
REGEXP_EXTRACT¶
Gets a string from a regular expression matching group.
- Syntax
REGEXP_EXTRACT(string1, string2[, integer])
- Description
The
REGEXP_EXTRACT
function returns a string fromstring1
that’s extracted with the regular expression specified instring2
and a regex match group index integer.The regex match group index starts from 1, and 0 specifies matching the whole regex.
The regex match group index must not exceed the number of the defined groups.
- Example
-- returns "bar" SELECT REGEXP_EXTRACT('foothebar', 'foo(.*?)(bar)', 2);
REGEXP_REPLACE¶
Replaces substrings in a string that match a regular expression.
- Syntax
REGEXP_REPLACE(string1, string2, string3)
- Description
- The
REGEXP_REPLACE
function returns a string fromstring1
with all of the substrings that match the regular expression instring2
consecutively replaced withstring3
. - Example
-- returns "fb" SELECT REGEXP_REPLACE('foobar', 'oo|ar', '');
REPEAT¶
Concatenates copies of a sstring.
- Syntax
REPEAT(string, integer)
- Description
- The
REPEAT
function returns a string that repeats the base stringinteger
times. - Example
-- returns "TestingTesting" SELECT REPEAT('Testing', 2);
REPLACE¶
Replace substrings in a string.
- Syntax
REPLACE(string1, string2, string3)
- Description
- The
REPLACE
function returns a new string that replaces all occurrences ofstring2
withstring3
(non-overlapping) fromstring1
. - Examples
-- returns "hello flink" SELECT REPLACE('hello world', 'world', 'flink'); -- returns "zab" SELECT REPLACE('ababab', 'abab', 'z');
REVERSE¶
Reverses a string.
- Syntax
REVERSE(string)
- Description
- The
REVERSE
function returns the reversed string. Returns NULL ifstring
is NULL. - Example
-- returns "xof nworb kciuq eht" SELECT REVERSE('the quick brown fox');
RIGHT¶
Gets the rightmost characters in a string.
- Syntax
RIGHT(string, integer)
- Description
- The
RIGHT
function returns the rightmostinteger
characters from the specified string. Returns an empty string ifinteger
is negative. Returns NULL if either argument is NULL. - Example
-- returns "Anderson" SELECT RIGHT('Thomas A. Anderson', 8);
- Related function
RPAD¶
Right-pad a string.
- Syntax
RPAD(string1, integer, string2)
- Description
The
RPAD
function returns a new string fromstring1
that’s right-padded withstring2
to a length ofinteger
characters.If the length of
string1
is shorter thaninteger
, returnsstring1
shortened tointeger
characters.To left-pad a string, use the LPAD function.
- Examples
-- returns "hi??" SELECT RPAD('hi', 4, '??'); -- returns "h" SELECT RPAD('hi', 1, '??');
- Related function
RTRIM¶
Removes right whitespaces from a string.
- Syntax
RTRIM(string)
- Description
The
RTRIM
function removes the right whitespaces from the specified string.To remove the left whitespaces from a string, use the LTRIM function.
- Example
-- returns "This is a test string." SELECT RTRIM('This is a test string. ');
SPLIT_INDEX¶
Splits a string by a delimiter.
- Syntax
SPLIT_INDEX(string1, string2, integer1)
- Description
- The
SPLIT_INDEX
function splitsstring1
by the delimiter instring2
and returns theinteger1
zero-based string of the split strings. Returns NULL ifinteger
is negative. Returns NULL if any of the arguments is NULL. - Example
-- returns "fox" SELECT SPLIT_INDEX('The quick brown fox', ' ', 3);
STR_TO_MAP¶
Creates a map from a list of key-value strings.
- Syntax
STR_TO_MAP(string1[, string2, string3])
- Description
The
STR_TO_MAP
function returns a map after splittingstring1
into key/value pairs using the pair delimiter specified instring2
. The default is','
. Thestring3
argument specifies the key-value delimiter. The default is'='
.Both the pair delimiter and the key-value delimiter are treated as regular expressions, so special characters, like
<([{\^-=$!|]})?*+.>)
, must be properly escaped before using as a delimiter literal.- Example
-- returns {a=1, b=2, c=3} SELECT STR_TO_MAP('a=1,b=2,c=3'); -- returns {a=1, b=2, c=3} SELECT STR_TO_MAP('a:1;b:2;c:3', ';', ':');
SUBSTRING¶
Finds a substring in a string.
- Syntax
SUBSTRING(string, integer1 [ FOR integer2 ])
- Description
The
SUBSTRING
function returns a substring of the specified string, starting from positioninteger1
with lengthinteger2
.If
integer2
isn’t specified, the substring runs to the end ofstring
.This function can be abbreviated to
SUBSTR(string, integer1[, integer2])
.- Examples
-- returns "fox" SELECT SUBSTR('The quick brown fox', 17); -- returns "The" SELECT SUBSTR('The quick brown fox', 1, 3);
TO_BASE64¶
Encodes a string to base64.
- Syntax
TO_BASE64(string)
- Description
- The
TO_BASE64
function returns the base64-encoded representation of the specified string. Returns NULL ifstring
is NULL. - Example
-- returns "aGVsbG8gd29ybGQ=" SELECT TO_BASE64('hello world');
- Related function
TRIM¶
Removes leading and/or trailing characters from a string.
- Syntax
TRIM([ BOTH | LEADING | TRAILING ] string1 FROM string2)
- Description
- The
TRIM
function returns a string that removes leading and/or trailing charactersstring2
fromstring1
.
- Examples
-- returns "The quick brown " SELECT TRIM(TRAILING 'fox' FROM 'The quick brown fox'); -- returns " quick brown fox" SELECT TRIM(LEADING 'The' FROM 'The quick brown fox'); -- returns " The quick brown fox " SELECT TRIM(BOTH 'yyy' FROM 'yyy The quick brown fox yyy');