String Functions¶
Note
Unless specified otherwise, all functions return NULL if at least one of the arguments is NULL.
These functions assume that input strings contain valid UTF-8 encoded Unicode code points. The behavior is undefined if they are not.
- ascii(string) integer ¶
Returns unicode code point of the first character of
string
. Returns 0 ifstring
is empty.
- bit_length(string/binary) integer ¶
Returns the bit length for the specified string column.
SELECT bit_length('123'); -- 24
- chr(n) varchar ¶
Returns the Unicode code point
n
as a single character string. Ifn < 0
, the result is an empty string. Ifn >= 256
, the result is equivalent to chr(n % 256
).
- concat_ws(separator, [string/array<string>, ]...) varchar ¶
Returns the concatenation result for
string
and all elements inarray<string>
, separated byseparator
. The first argument isseparator
whose type is VARCHAR. Then, this function can take variable number of remaining arguments , and it allows mixed use ofstring
type andarray<string>
type. Skips NULL argument or NULL array element during the concatenation. Ifseparator
is NULL, returns NULL, regardless of the following inputs. For non-NULLseparator
, if no remaining input exists or all remaining inputs are NULL, returns an empty string.SELECT concat_ws('~', 'a', 'b', 'c'); -- 'a~b~c' SELECT concat_ws('~', ['a', 'b', 'c'], ['d']); -- 'a~b~c~d' SELECT concat_ws('~', 'a', ['b', 'c']); -- 'a~b~c' SELECT concat_ws('~', '', [''], ['a', '']); -- '~~a~' SELECT concat_ws(NULL, 'a'); -- NULL SELECT concat_ws('~'); -- '' SELECT concat_ws('~', NULL, [NULL], 'a', 'b'); -- 'a~b' SELECT concat_ws('~', NULL, NULL); -- '' SELECT concat_ws('~', [NULL]); -- ''
- contains(left, right) boolean ¶
Returns true if ‘right’ is found in ‘left’. Otherwise, returns false.
SELECT contains('Spark SQL', 'Spark'); -- true SELECT contains('Spark SQL', 'SPARK'); -- false SELECT contains('Spark SQL', null); -- NULL SELECT contains(x'537061726b2053514c', x'537061726b'); -- true
- conv(number, fromBase, toBase) varchar ¶
Converts
number
represented as a string fromfromBase
totoBase
.fromBase
must be an INTEGER value between 2 and 36 inclusively.toBase
must be an INTEGER value between 2 and 36 inclusively or between -36 and -2 inclusively. Otherwise, returns NULL. Returns a signed number iftoBase
is negative. Otherwise, returns an unsigned one. Returns NULL ifnumber
is empty. Skips leading spaces.number
may contain other characters not valid forfromBase
. All characters starting from the first invalid character till the end of the string are ignored. Only converts valid characters even thoughfromBase
=toBase
. Returns ‘0’ if no valid character is found.SELECT conv('100', 2, 10); -- '4' SELECT conv('-10', 16, -10); -- '-16' SELECT conv("-1", 10, 16); -- 'FFFFFFFFFFFFFFFF' SELECT conv("123", 10, 39); -- NULL SELECT conv('', 16, 10); -- NULL SELECT conv(' ', 2, 10); -- NULL SELECT conv("11", 10, 16); -- 'B' SELECT conv("11ABC", 10, 16); -- 'B' SELECT conv("11abc", 10, 10); -- '11' SELECT conv('H016F', 16, 10); -- '0'
- empty2null(input) varchar ¶
Returns NULL if
input
is empty. Otherwise, returnsinput
. Note: it’s an internal Spark function used to convert empty value of a partition column, which is then converted to Hive default partition value__HIVE_DEFAULT_PARTITION__
.SELECT empty2null(''); -- NULL SELECT empty2null('abc'); -- 'abc'
- endswith(left, right) boolean ¶
Returns true if ‘left’ ends with ‘right’. Otherwise, returns false.
SELECT endswith('js SQL', 'SQL'); -- true SELECT endswith('js SQL', 'js'); -- false SELECT endswith('js SQL', NULL); -- NULL
- find_in_set(str, strArray) integer ¶
Returns 1-based index of the given string
str
in the comma-delimited liststrArray
. Returns 0, if the string was not found or if the given stringstr
contains a comma.SELECT find_in_set('ab', 'abc,b,ab,c,def'); -- 3 SELECT find_in_set('ab,', 'abc,b,ab,c,def'); -- 0 SELECT find_in_set('dfg', 'abc,b,ab,c,def'); -- 0 SELECT find_in_set('', ''); -- 1 SELECT find_in_set('', '123,'); -- 2 SELECT find_in_set('', ',123'); -- 1 SELECT find_in_set(NULL, ',123'); -- NULL SELECT find_in_set("abc", NULL); -- NULL
- instr(string, substring) integer ¶
Returns the starting position of the first instance of
substring
instring
. Positions start with1
. Returns 0 if ‘substring’ is not found.
- left(string, length) string ¶
Returns the leftmost length characters from the
string
. Iflength
is less or equal than 0 the result is an empty string.
- length(string) integer ¶
Returns the length of
string
in characters.
- levenshtein(string1, string2[, threshold]) integer ¶
Returns the Levenshtein distance between the two given strings. If the provided
threshold
is negative, or the levenshtein distance exceedsthreshold
, returns -1.SELECT levenshtein('kitten', 'sitting'); -- 3 SELECT levenshtein('kitten', 'sitting', 10); -- 3 SELECT levenshtein('kitten', 'sitting', 2); -- -1
- locate(substring, string, start) integer ¶
Returns the 1-based position of the first occurrence of
substring
in givenstring
after positionstart
. The search is from the beginning ofstring
to the end.start
is the starting character position instring
to search for thesubstring
.start
is 1-based and must be at least 1 and at most the characters number ofstring
. The following rules on special values are applied to follow Spark’s implementation. They are listed in order of priority:Returns 0 if
start
is NULL. Returns NULL ifsubstring
orstring
is NULL. Returns 0 ifstart
is less than 1. Returns 1 ifsubstring
is empty. Returns 0 ifstart
is greater than the characters number ofstring
. Returns 0 ifsubstring
is not found instring
.SELECT locate('aa', 'aaads', 1); -- 1 SELECT locate('aa', 'aaads', -1); -- 0 SELECT locate('aa', 'aaads', 2); -- 2 SELECT locate('aa', 'aaads', 6); -- 0 SELECT locate('aa', 'aaads', NULL); -- 0 SELECT locate('', 'aaads', 1); -- 1 SELECT locate('', 'aaads', 9); -- 1 SELECT locate('', 'aaads', -1); -- 0 SELECT locate('', '', 1); -- 1 SELECT locate('aa', '', 1); -- 0 SELECT locate(NULL, NULL, NULL); -- 0 SELECT locate(NULL, NULL, 1); -- NULL SELECT locate('\u4FE1', '\u4FE1\u5FF5,\u4FE1\u7231,\u4FE1\u5E0C\u671B', 2); -- 4
- lower(string) string ¶
Returns string with all characters changed to lowercase.
SELECT lower('SparkSql'); -- sparksql
- lpad(string, len, pad) string ¶
Returns
string
, left-padded with pad to a length oflen
. Ifstring
is longer thanlen
, the return value is shortened tolen
characters or bytes. Ifpad
is not specified,string
will be padded to the left with space characters if it is a character string, and with zeros if it is a byte sequence.SELECT lpad('hi', 5, '??'); -- ???hi SELECT lpad('hi', 1, '??'); -- h SELECT lpad('hi', 4); -- hi
- ltrim(string) varchar ¶
Removes leading 0x20(space) characters from
string
.SELECT ltrim(' data '); -- "data "
- ltrim(trimCharacters, string) varchar
Removes specified leading characters from
string
. The specified character is any character contained intrimCharacters
.trimCharacters
can be empty and may contain duplicate characters.SELECT ltrim('ps', 'spark'); -- "ark"
- mask(string[, upperChar, lowerChar, digitChar, otherChar]) string ¶
Returns a masked version of the input
string
.string
: string value to mask.upperChar
: A single character string used to substitute upper case characters. The default is ‘X’. If NULL, upper case characters remain unmasked.lowerChar
: A single character string used to substitute lower case characters. The default is ‘x’. If NULL, lower case characters remain unmasked.digitChar
: A single character string used to substitute digits. The default is ‘n’. If NULL, digits remain unmasked.otherChar
: A single character string used to substitute any other character. The default is NULL, which leaves these characters unmasked. Any invalid UTF-8 characters present in the input string will be treated as a single other character.SELECT mask('abcd-EFGH-8765-4321'); -- "xxxx-XXXX-nnnn-nnnn" SELECT mask('abcd-EFGH-8765-4321', 'Q'); -- "xxxx-QQQQ-nnnn-nnnn" SELECT mask('AbCD123-@$#'); -- "XxXXnnn-@$#" SELECT mask('AbCD123-@$#', 'Q'); -- "QxQQnnn-@$#" SELECT mask('AbCD123-@$#', 'Q', 'q'); -- "QqQQnnn-@$#" SELECT mask('AbCD123-@$#', 'Q', 'q', 'd'); -- "QqQQddd-@$#" SELECT mask('AbCD123-@$#', 'Q', 'q', 'd', 'o'); -- "QqQQdddoooo" SELECT mask('AbCD123-@$#', NULL, 'q', 'd', 'o'); -- "AqCDdddoooo" SELECT mask('AbCD123-@$#', NULL, NULL, 'd', 'o'); -- "AbCDdddoooo" SELECT mask('AbCD123-@$#', NULL, NULL, NULL, 'o'); -- "AbCD123oooo" SELECT mask(NULL, NULL, NULL, NULL, 'o'); -- NULL SELECT mask(NULL); -- NULL SELECT mask('AbCD123-@$#', NULL, NULL, NULL, NULL); -- "AbCD123-@$#"
- overlay(input, replace, pos, len) same as input ¶
Replace a substring of
input
starting atpos
character withreplace
and going for restlen
characters ofinput
. Types ofinput
andreplace
must be the same. Supported types are: VARCHAR and VARBINARY. Wheninput
types are VARCHAR,len
andpos
are specified in characters, otherwise, bytes. Result is constructed from three parts. First part is first pos - 1 characters ofinput
whenpos
if greater then zero, otherwise, empty string. Second part isreplace
. Third part is rest ofinput
from indices pos + len which is 1-based, iflen
is negative, it will be set to size ofreplace
, if pos + len is negative, it refers to -(pos + len)th element before the end ofinput
.SELECT overlay('Spark SQL', '_', 6, -1); -- "Spark_SQL" SELECT overlay('Spark SQL', 'CORE', 7, -1); -- "Spark CORE" SELECT overlay('Spark SQL', 'ANSI ', 7, 0); -- "Spark ANSI SQL" SELECT overlay('Spark SQL', 'tructured', 2, 4); -- "Structured SQL" SELECT overlay('Spark SQL', '_', -6, 3); -- "_Sql"
- repeat(input, n) varchar ¶
Returns the string which repeats
input
n
times. Result size must be less than or equal to 1MB. Ifn
is less than or equal to 0, empty string is returned.SELECT repeat('123', 2); -- 123123
- replace(input, replaced) varchar ¶
Removes all instances of
replaced
frominput
. Ifreplaced
is an empty string, returns the originalinput
string.SELECT replace('ABCabc', ''); -- ABCabc SELECT replace('ABCabc', 'bc'); -- ABCc
- replace(input, replaced, replacement) varchar ¶
Replaces all instances of
replaced
withreplacement
ininput
. Ifreplaced
is an empty string, returns the originalinput
string.SELECT replace('ABCabc', '', 'DEF'); -- ABCabc SELECT replace('ABCabc', 'abc', ''); -- ABC SELECT replace('ABCabc', 'abc', 'DEF'); -- ABCDEF
- reverse(string) varchar ¶
Returns input string with characters in reverse order.
- rpad(string, len, pad) string ¶
Returns
string
, right-padded withpad
to a length oflen
. Ifstring
is longer thanlen
, the return value is shortened tolen
characters. Ifpad
is not specified,string
will be padded to the right with space characters if it is a character string, and with zeros if it is a binary string.SELECT lpad('hi', 5, '??'); -- ???hi SELECT lpad('hi', 1, '??'); -- h SELECT lpad('hi', 4); -- hi
- rtrim(string) varchar ¶
Removes trailing 0x20(space) characters from
string
.SELECT rtrim(' data '); -- " data"
- rtrim(trimCharacters, string) varchar
Removes specified trailing characters from
string
. The specified character is any character contained intrimCharacters
.trimCharacters
can be empty and may contain duplicate characters.SELECT rtrim('kr', 'spark'); -- "spa"
- soundex(string) string ¶
Returns Soundex code of the string. If first character of
string
is not a letter,string
is returned.SELECT soundex('Miller'); -- "M460"
- split(string, delimiter[, limit]) -> array(string)¶
Splits
string
around occurrences that matchdelimiter
and returns an array with a length of at mostlimit
.delimiter
is a string representing regular expression.limit
is an integer which controls the number of times the regex is applied. By default,limit
is -1. Whenlimit
> 0, the resulting array’s length will not be more thanlimit
, and the resulting array’s last entry will contain all input beyond the last matched regex. Whenlimit
<= 0,regex
will be applied as many times as possible, and the resulting array can be of any size. Whendelimiter
is empty, iflimit
is smaller than the size ofstring
, the resulting array only containslimit
number of single characters splitting fromstring
, iflimit
is not provided or is larger than the size ofstring
, the resulting array contains all the single characters ofstring
and does not include an empty tail character. The split function align with vanilla spark 3.4+ split function.SELECT split('oneAtwoBthreeC', '[ABC]'); -- ["one","two","three",""] SELECT split('oneAtwoBthreeC', '[ABC]', 2); -- ["one","twoBthreeC"] SELECT split('oneAtwoBthreeC', '[ABC]', 5); -- ["one","two","three",""] SELECT split('one', '1'); -- ["one"] SELECT split('abcd', ''); -- ["a","b","c","d"] SELECT split('abcd', '', 3); -- ["a","b","c"] SELECT split('abcd', '', 5); -- ["a","b","c","d"]
- startswith(left, right) boolean ¶
Returns true if ‘left’ starts with ‘right’. Otherwise, returns false.
SELECT startswith('js SQL', 'js'); -- true SELECT startswith('js SQL', 'SQL'); -- false SELECT startswith('js SQL', null); -- NULL
- str_to_map(string, entryDelimiter, keyValueDelimiter)¶
Returns a map by splitting
string
into entries withentryDelimiter
and splitting each entry into key/value withkeyValueDelimiter
.entryDelimiter
andkeyValueDelimiter
must be constant strings with single ascii character. AllowskeyValueDelimiter
not found when splitting an entry. Throws exception when duplicate map keys are found for single row’s result, consistent with Spark’s default behavior.SELECT str_to_map('a:1,b:2,c:3', ',', ':'); -- {"a":"1","b":"2","c":"3"} SELECT str_to_map('a', ',', ':'); -- {"a":NULL} SELECT str_to_map('', ',', ':'); -- {"":NULL} SELECT str_to_map('a:1,b:2,c:3', ',', ','); -- {"a:1":NULL,"b:2":NULL,"c:3":NULL}
- substring(string, start) varchar ¶
Returns the rest of
string
from the starting positionstart
. Positions start with1
. A negative starting position is interpreted as being relative to the end of the string. When the starting position is 0, the meaning is to refer to the first character.Type of ‘start’ must be an INTEGER.
- substring(string, start, length) varchar
Returns a substring from
string
of lengthlength
from the starting positionstart
. Positions start with1
. A negative starting position is interpreted as being relative to the end of the string. When the starting position is 0, the meaning is to refer to the first character. Type of ‘start’ must be an INTEGER.SELECT substring('Spark SQL', 0, 2); -- Sp SELECT substring('Spark SQL', 1, 2); -- Sp SELECT substring('Spark SQL', 5, 0); -- "" SELECT substring('Spark SQL', 5, -1); -- "" SELECT substring('Spark SQL', 5, 10000); -- "k SQL" SELECT substring('Spark SQL', -9, 3); -- "Spa" SELECT substring('Spark SQL', -10, 3); -- "Sp" SELECT substring('Spark SQL', -20, 3); -- ""
- substring_index(string, delim, count) [same as string] ¶
Returns the substring from
string
beforecount
occurrences of the delimiterdelim
. Here thestring
can be VARCHAR or VARBINARY and return type matches type ofstring
. Ifcount
is positive, returns everything to the left of the final delimiter (counting from the left). Ifcount
is negative, returns everything to the right of the final delimiter (counting from the right). Ifcount
is 0, returns empty string. Ifdelim
is not found or found fewer times thancount
, returns the original input string.delim
is case-sensitive. It also takes into account overlapping strings.SELECT substring_index('Spark.SQL', '.', 1); -- "Spark" SELECT substring_index('Spark.SQL', '.', 0); -- "" SELECT substring_index('Spark.SQL', '.', -1); -- "SQL" SELECT substring_index('TEST.Spark.SQL', '.',2); -- "TEST.Spark" SELECT substring_index('TEST.Spark.SQL', '', 0); -- "" SELECT substring_index('TEST.Spark.SQL', '.', -2); -- "Spark.SQL" SELECT substring_index('TEST.Spark.SQL', '.', 10); -- "TEST.Spark.SQL" SELECT substring_index('TEST.Spark.SQL', '.', -12); -- "TEST.Spark.SQL" SELECT substring_index('aaaaa', 'aa', 2); -- "a" SELECT substring_index('aaaaa', 'aa', -4); -- "aaa" SELECT substring_index('aaaaa', 'aa', 0); -- "" SELECT substring_index('aaaaa', 'aa', 5); -- "aaaaa" SELECT substring_index('aaaaa', 'aa', -5); -- "aaaaa"
- translate(string, match, replace) varchar ¶
Returns a new translated string. It translates the character in
string
by a character inreplace
. The character inreplace
is corresponding to the character inmatch
. The translation will happen when any character instring
matching with a character inmatch
. Ifmatch's
character size is larger thanreplace's
, the extra characters inmatch
will be removed fromstring
. In addition, this function only considers the first occurrence of a character inmatch
and uses its corresponding character inreplace
for translation. Any invalid UTF-8 characters present in the input string will be treated as a single character.:SELECT translate('spark', 'sa', '12'); -- "1p2rk" SELECT translate('spark', 'sa', '1'); -- "1prk" SELECT translate('spark', 'ss', '12'); -- "1park"
- trim(string) varchar ¶
Removes leading and trailing 0x20(space) characters from
string
.SELECT trim(' data '); -- "data"
- trim(trimCharacters, string) varchar
Removes specified leading and trailing characters from
string
. The specified character is any character contained intrimCharacters
.trimCharacters
can be empty and may contain duplicate characters.SELECT trim('sprk', 'spark'); -- "a"
- upper(string) string ¶
Returns string with all characters changed to uppercase.
SELECT upper('SparkSql'); -- SPARKSQL