字符串函数
更新时间:2022-09-13
PALO支持的字符串函数如下:
1.append_trailing_char_if_absent
2.ascii
3.concat
4.concat_ws
5.ends_with
6.find_in_set
7.group_concat
8.instr
9.length,char_length,character_length
10.locate
11.lower,lcase
12.lpad
13.ltrim
14.money_format
15.null_or_empty
16.parse_url
17.regexp_extract
18.regexp_replace
19.repeat
20.replace
21.reverse
22.rpad
23.rtrim
24.space
25.split_part
26.starts_with
27.strleft,left
28.strright,right
29.substr,substring
30.trim
31.upper,ucase
APPEND_TRAILING_CHAR_IF_ABSENT
Description
SQL
1append_trailing_char_if_absent(VARCHAR str, VARCHAR trailing_char)
- 功能:如果str字符串非空并且末尾不包含trailing_char字符,则将trailing_char字符附加到末尾。 trailing_char只包含一个字符,如果包含多个字符,将返回NULL
- 返回类型:字符串类型
Example
SQL
1mysql> select append_trailing_char_if_absent('abcde', 'a');
2+----------------------------------------------+
3| append_trailing_char_if_absent('abcde', 'a') |
4+----------------------------------------------+
5| abcdea |
6+----------------------------------------------+
7
8mysql> select append_trailing_char_if_absent('abcde', 'ab');
9+-----------------------------------------------+
10| append_trailing_char_if_absent('abcde', 'ab') |
11+-----------------------------------------------+
12| NULL |
13+-----------------------------------------------+
Keywords
Plain Text
1append_trailing_char_if_absent
ASCII
Description
SQL
1ascii(string str)
- 功能:返回字符串第一个字符串对应的ascii 码
- 返回类型:int类型
Example
SQL
1mysql> select ascii('doris');
2+---------------+
3| ascii('doris') |
4+---------------+
5| 112 |
6+---------------+
7
8mysql> select ascii('doris and doris');
9+-------------------------+
10| ascii('doris and doris') |
11+-------------------------+
12| 112 |
13+-------------------------+
Keywords
Plain Text
1ascii
CONCAT
Description
SQL
1concat(string a, string b...)
- 功能:将多个字符串连接起来
- 返回类型:string类型
- 使用说明:concat()和concat_ws()都是将一行中的多个列合成1个新的列,group_concat()是聚合函数,将不同行的结果合成1个新的列
Example
SQL
1mysql> select concat('The date today is ',to_date(now()));
2+----------------------------------------------+
3| concat('The date today is ', to_date(now())) |
4+----------------------------------------------+
5| The date today is 2020-12-29 |
6+----------------------------------------------+
Keywords
Plain Text
1concat
CONCAT_WS
Description
SQL
1concat_ws(string sep, string a, string b...)
- 功能:将第二个参数以及后面的参数连接起来,连接符为第一个参数。
- 返回类型:string类型
Example
SQL
1mysql> select concat_ws('a', 'b', 'c', 'd');
2+-------------------------------+
3| concat_ws('a', 'b', 'c', 'd') |
4+-------------------------------+
5| bacad |
6+-------------------------------+
Keywords
Plain Text
1concat_ws
ENDS_WITH
Description
SQL
1ends_with(string str, string strEnd)
- 功能:判断str是否以strEnd结尾
- 返回类型:bool类型
Example
SQL
1mysql> select ends_with('today','y');
2+-------------------------+
3| ends_with('today', 'y') |
4+-------------------------+
5| 1 |
6+-------------------------+
Keywords
Plain Text
1ends_with
FIND_IN_SET
Description
SQL
1find_in_set(string str, string strList)
- 功能:返回strlist中出现第一次str的位置(从1开始计数)。strList用逗号分隔多个字符串。如果在strList中没有找到str,则返回0。
- 返回类型:int类型
Example
SQL
1mysql> select find_in_set("beijing", "tianji,beijing,shanghai");
2+---------------------------------------------------+
3| find_in_set('beijing', 'tianji,beijing,shanghai') |
4+---------------------------------------------------+
5| 2 |
6+---------------------------------------------------+
Keywords
Plain Text
1find_in_set
GROUP_CONCAT
Description
SQL
1group_concat(string s [, string sep])
- 功能:该函数是类似于sum()的聚合函数,group_concat将结果集中的多行结果连接成一个字符串。第二个参数为字符串之间的连接符号,该参数可以省略。该函数通常需要和group by 语句一起使用。
- 返回类型:string类型
Example
SQL
1mysql> select k1, group_concat(k2) from tbl group by k1;
2+----+------------------+
3| k1 | group_concat(k2) |
4+-----------------------+
5| 1 | 1,2,3,4 |
6+-----------------------+
7| 1 | 5,6,7,8 |
8+-----------------------+
Keywords
Plain Text
1group_concat
INSTR
Description
SQL
1instr(string str, string substr)
- 功能:返回substr在str中第一次出现的位置(从1开始计数)。如果substr不在str中出现,则返回0。
- 返回类型:int类型
Example
SQL
1mysql> select instr('foo bar bletch', 'b');
2+------------------------------+
3| instr('foo bar bletch', 'b') |
4+------------------------------+
5| 5 |
6+------------------------------+
7
8mysql> select instr('foo bar bletch', 'z');
9+------------------------------+
10| instr('foo bar bletch', 'z') |
11+------------------------------+
12| 0 |
13+------------------------------+
Keywords
Plain Text
1instr
LENGTH
Description
SQL
1length(string a)
2
3char_length(string a)
4
5character_length(string a)
- 功能:返回字符串的长度。其中
length
返回字节长度,而char(acter)_length
返回字符长度。 - 返回类型:int类型
Example
SQL
1mysql> select length('today');
2+-----------------+
3| length('today') |
4+-----------------+
5| 5 |
6+-----------------+
7
8mysql> select length("中国");
9+------------------+
10| length('中国') |
11+------------------+
12| 6 |
13+------------------+
14
15mysql> select char_length("中国");
16+-----------------------+
17| char_length('中国') |
18+-----------------------+
19| 2 |
20+-----------------------+
注:UTF-8 编码,一个汉字占 3 个字节。
Keywords
Plain Text
1length, char_length, character_length
LOCATE
Description
SQL
1locate(string substr, string str[, int pos])
- 功能:返回substr在str中出现的位置(从1开始计数)。如果指定第3个参数,则从str以pos下标开始的字符串处开始查找substr出现的位置。
- 返回类型:int类型
Example
SQL
1mysql> select locate('bj', 'where is bj', 10);
2+---------------------------------+
3| locate('bj', 'where is bj', 10) |
4+---------------------------------+
5| 10 |
6+---------------------------------+
7
8mysql> select locate('bj', 'where is bj', 11);
9+---------------------------------+
10| locate('bj', 'where is bj', 11) |
11+---------------------------------+
12| 0 |
13+---------------------------------+
Keywords
Plain Text
1locate
LOWER,LCASE
Description
SQL
1lower(string a)
2
3lcase(string a)
- 功能:将参数中所有的字符串都转换成小写
- 返回类型:string类型
Example
SQL
1mysql> select lower('toDAY Is FridAy');
2+--------------------------+
3| lower('toDAY Is FridAy') |
4+--------------------------+
5| today is friday |
6+--------------------------+
7
8mysql> select lcase('toDAY Is FridAy');
9+--------------------------+
10| lcase('toDAY Is FridAy') |
11+--------------------------+
12| today is friday |
13+--------------------------+
Keywords
Plain Text
1lower,lcase
LPAD
Description
SQL
1lpad(string str, int len, string pad)
- 功能:返回str中长度为len(从首字母开始算起)的字符串。如果len大于str的长度,则在str的前面不断补充pad字符,直到该字符串的长度达到len为止。如果len小于str的长度,该函数相当于截断str字符串,只返回长度为len的字符串。
- 返回类型:string类型
Example
SQL
1 mysql> select lpad('aoaoaoao',10,'xy');
2+----------------------------+
3| lpad('aoaoaoao', 10, 'xy') |
4+----------------------------+
5| xyaoaoaoao |
6+----------------------------+
7
8mysql> select lpad('aoaoaoao',6,'xy');
9+---------------------------+
10| lpad('aoaoaoao', 6, 'xy') |
11+---------------------------+
12| aoaoao |
13+---------------------------+
Keywords
Plain Text
1lpad
LTRIM
Description
SQL
1ltrim(string a)
- 功能:将参数中从开始部分连续出现的空格去掉。
- 返回类型:string类型
Example
SQL
1mysql> select ltrim(' today is friday');
2+------------------------------+
3| ltrim(' today is friday') |
4+------------------------------+
5| today is friday |
6+------------------------------+
Keywords
Plain Text
1ltrim
MONEY_FORMAT
Description
SQL
1money_format(numric money)
- 功能:转换为金钱格式
- 返回类型:string类型
Example
SQL
1select money_format(11111);
2+---------------------+
3| money_format(11111) |
4+---------------------+
5| 11,111.00 |
6+---------------------+
Keywords
Plain Text
1money_format
NULL_OR_EMPTY
Description
SQL
1null_or_empty(string str)
- 功能:判断str是否为NULL或空字符串
- 返回类型:bool类型
Example
SQL
1mysql> select null_or_empty('');
2+-------------------+
3| null_or_empty('') |
4+-------------------+
5| 1 |
6+-------------------+
7
8mysql> select null_or_empty('today');
9+------------------------+
10| null_or_empty('today') |
11+------------------------+
12| 0 |
13+------------------------+
Keywords
Plain Text
1null_or_empty
PARSE_URL
Description
SQL
1parse_url(string url, string name)
- 功能:在url解析出name对应的字段,name可选项为:'PROTOCOL', 'HOST', 'PATH', 'REF', 'AUTHORITY', 'FILE', 'USERINFO', 'PORT', 'QUERY',将结果返回。
- 返回类型:string类型
Example
SQL
1mysql> select parse_url ('https://cloud.baidu.com/product/doris.html', 'PROTOCOL');
2+--------------------------------------------------------------------+
3| parse_url('https://cloud.baidu.com/product/doris.html', 'PROTOCOL') |
4+--------------------------------------------------------------------+
5| https |
6+--------------------------------------------------------------------+
71 row in set (0.02 sec)
Keywords
Plain Text
1parse_url
REGEXP_EXTRACT
Description
SQL
1regexp_extract(string subject, string pattern, int index)
- 功能:字符串正则匹配。index为0返回整个匹配的字符串,index为1,2,……,返回第一,第二,……部分。
- 返回类型:string类型
Example
SQL
1mysql> select regexp_extract('AbcdBCdefGHI','.*?([[:lower:]]+)',1);
2+--------------------------------------------------------+
3| regexp_extract('AbcdBCdefGHI', '.*?([[:lower:]]+)', 1) |
4+--------------------------------------------------------+
5| def |
6+--------------------------------------------------------+
7
8mysql> select regexp_extract('AbcdBCdefGHI','.*?([[:lower:]]+).*?',1);
9+-----------------------------------------------------------+
10| regexp_extract('AbcdBCdefGHI', '.*?([[:lower:]]+).*?', 1) |
11+-----------------------------------------------------------+
12| bcd |
13+-----------------------------------------------------------+
Keywords
Plain Text
1regexp_extract, regexp
REGEXP_REPLACE
Description
SQL
1regexp_replace(string initial, string pattern, string replacement)
- 功能:用replacement替换initial字符串中匹配pattern的部分。
- 返回类型:string类型
Example
SQL
1mysql> select regexp_replace('aaabbbaaa','b+','xyz');
2+------------------------------------------+
3| regexp_replace('aaabbbaaa', 'b+', 'xyz') |
4+------------------------------------------+
5| aaaxyzaaa |
6+------------------------------------------+
7
8mysql> select regexp_replace('aaabbbaaa','(b+)','<\\1>');
9+---------------------------------------------+
10| regexp_replace('aaabbbaaa', '(b+)', '<\1>') |
11+---------------------------------------------+
12| aaa<bbb>aaa |
13+---------------------------------------------+
14
15mysql> select regexp_replace('123-456-789','[^[:digit:]]','');
16+---------------------------------------------------+
17| regexp_replace('123-456-789', '[^[:digit:]]', '') |
18+---------------------------------------------------+
19| 123456789 |
20+---------------------------------------------------+
Keywords
Plain Text
1regexp_replace, regexp
REPEAT
Description
SQL
1repeat(string str, int n)
- 功能:返回字符串str重复n次的结果
- 返回类型:string类型
Example
SQL
1mysql> select repeat("abc", 3);
2+------------------+
3| repeat('abc', 3) |
4+------------------+
5| abcabcabc |
6+------------------+
Keywords
Plain Text
1repeat
REPLACE
Description
SQL
1replace(string oriStr, string src, string dest)
- 功能:oriStr中所有的src替换成dest,结果作为返回值。注意和regexp_replace()的区别,replace()是完全匹配字符串,regexp_replace()可支持表达式。
- 返回类型:string类型
Example
SQL
1mysql> select replace('aaabbbaaa','b+','xyz');
2+-----------------------------------+
3| replace('aaabbbaaa', 'b+', 'xyz') |
4+-----------------------------------+
5| aaabbbaaa |
6+-----------------------------------+
7
8mysql> select replace('aaabbbaaa','bb','xyz');
9+-----------------------------------+
10| replace('aaabbbaaa', 'bb', 'xyz') |
11+-----------------------------------+
12| aaaxyzbaaa |
13+-----------------------------------+
Keywords
Plain Text
1replace
REVERSE
Description
SQL
1reverse(string a)
- 功能:将字符串反转
- 返回类型:string类型
Example
SQL
1mysql> select reverse('doris');
2+-----------------+
3| reverse('doris') |
4+-----------------+
5| olap |
6+-----------------+
Keywords
Plain Text
1reverse
RPAD
Description
SQL
1rpad(string str, int len, string pad)
- 功能:返回str中长度为len(从首字母开始算起)的字符串。如果len大于str的长度,则在str 的后面不断补充pad字符,直到该字符串的长度达到len 为止。如果len小于str的长度,该函数相当于截断str字符串,只返回长度为len的字符串。
- 返回类型:string类型
Example
SQL
1mysql> select rpad("hello", 10, 'xy');
2+-------------------------+
3| rpad('hello', 10, 'xy') |
4+-------------------------+
5| helloxyxyx |
6+-------------------------+
Keywords
Plain Text
1rpad
RTRIM
Description
SQL
1rtrim(string a)
- 功能:将参数中从右侧部分部分连续出现的空格去掉。可以和ltrim()对比看出功能的不同之处。一个是去掉字符串前面的空格,一个是去掉字符串后面的空格。
- 返回类型:string类型
Example
SQL
1mysql> select rtrim(' today is friday ');
2+---------------------------------+
3| rtrim(' today is friday ') |
4+---------------------------------+
5| today is friday |
6+---------------------------------+
7
8mysql> select ltrim(' today is friday ');
9+---------------------------------+
10| ltrim(' today is friday ') |
11+---------------------------------+
12| today is friday |
13+---------------------------------+
Keywords
Plain Text
1rtrim
SPACE
Description
SQL
1space(int n)
- 功能:返回n个空格的字符串
- 返回类型:string类型
Example
SQL
1mysql> select space(10);
2+------------+
3| space(10) |
4+------------+
5| |
6+------------+
7
8mysql> select space(20);
9+----------------------+
10| space(20) |
11+----------------------+
12| |
13+----------------------+
Keywords
Plain Text
1space
SPLIT_PART
Description
SQL
1split_part(string str, string splitStr, int num)
- 功能:str按照splitStr进行切分,返回第num个值
- 返回类型:string类型
Example
SQL
1select split_part('12,31,45,232', ',', 3);
2+------------------------------------+
3| split_part('12,31,45,232', ',', 3) |
4+------------------------------------+
5| 45 |
6+------------------------------------+
Keywords
Plain Text
1split_part
STARTS_WITH
Description
SQL
1starts_with(string str, string strPrefix)
- 功能:判断str是否以strPrefix开始
- 返回类型:bool类型
Example
SQL
1mysql> select starts_with('baidu doris','doris');
2+-----------------------------------+
3| starts_with('baidu doris', 'doris') |
4+-----------------------------------+
5| 0 |
6+-----------------------------------+
7
8mysql> select starts_with('baidu doris','baidu');
9+------------------------------------+
10| starts_with('baidu doris', 'baidu') |
11+------------------------------------+
12| 1 |
13+------------------------------------+
Keywords
Plain Text
1starts_with
STRLEFT,LEFT
Description
SQL
1strleft(string a, int num_chars)
2
3left(string a, int num_chars)
- 功能:返回字符串中最左边的num_chars个字符。
- 返回类型:string类型
Example
SQL
1mysql> select strleft('doris@baidu',5);
2+--------------------------+
3| strleft('doris@baidu', 5) |
4+--------------------------+
5| doris@ |
6+--------------------------+
7
8mysql> select left('doris@baidu',4);
9+-----------------------+
10| left('doris@baidu', 4) |
11+-----------------------+
12| doris |
13+-----------------------+
Keywords
Plain Text
1strleft,left
STRRIGHT,RIGHT
Description
SQL
1strright(string a, int num_chars)
2
3right(string a, int num_chars)
- 功能:返回字符串中最右边的num_chars个字符。
- 返回类型:string类型
Example
SQL
1mysql> select strright('doris@baidu',5);
2+---------------------------+
3| strright('doris@baidu', 5) |
4+---------------------------+
5| baidu |
6+---------------------------+
7
8mysql> select right('doris@baidu',6);
9+------------------------+
10| right('doris@baidu', 6) |
11+------------------------+
12| @baidu |
13+------------------------+
Keywords
Plain Text
1strright,right
SUBSTR,SUBSTRING
Description
SQL
1substr(string a, int start [, int len])
2
3substring(string a, int start[, int len])
- 功能:求子串函数,返回第一个参数描述的字符串中从start开始长度为len的部分字符串。首字母的下标为1。
- 返回类型:string类型
Example
SQL
1mysql> select substring('baidudoris',6);
2+---------------------------+
3| substring('baidudoris', 6) |
4+---------------------------+
5| doris |
6+---------------------------+
Keywords
Plain Text
1substr,substring
TRIM
Description
SQL
1trim(string a)
- 功能:将参数中右侧部分连续出现的空格和左侧部分连续出现的空格都去掉。该函数的效果和同时使用ltrim()和rtrim()的效果是一样的。
- 返回类型:string类型
Example
SQL
1mysql> select trim(' today is friday ');
2+--------------------------------+
3| trim(' today is friday ') |
4+--------------------------------+
5| today is friday |
6+--------------------------------+
Keywords
Plain Text
1trim
UPPER,UCASE
Description
SQL
1upper(string a)
2
3ucase(string a)
- 功能:将字符串所有字母都转换成大写。
- 返回类型:string类型
Example
SQL
1mysql> select upper('toDAY Is FridAy');
2+--------------------------+
3| upper('toDAY Is FridAy') |
4+--------------------------+
5| TODAY IS FRIDAY |
6+--------------------------+
7
8mysql> select ucase('doris');
9+---------------+
10| ucase('doris') |
11+---------------+
12| DORIS |
13+---------------+
Keywords
Plain Text
1upper, ucase