ImpalaString函数⼤全
Impala字符串函数
Impala中字符串函数主要应⽤于 varchar、char、string类型,如果把varchar或者char类型的值传递给字符串函数,返回将是⼀个string 类型的值
函数列表
base64encode(string str)
base64decode(string str)
加密和解密,返回值为4字节的倍数,可以⽤来存储特殊字符串
--将hello world加密
[master:21000] > select base64encode('hello world') as encoded;
+------------------+
| encoded |
+------------------+
| aGVsbG8gd29ybGQ= |
+------------------+
--将加密后的密⽂解密
[master:21000] > select base64decode('aGVsbG8gd29ybGQ=') as decoded;
+-------------+
| decoded |
+-------------+
| hello world |
+-------------+
ascii(string str)
返回参数字符串的第⼀个字符的ascii码
--得到字符a的ascii码
[master:21000] > select ascii('a') as ascii;
+-------+
| ascii |
+-------+
| 97 |
+-------+
--验证是否只能返回第⼀个字符
[master:21000] > select ascii('abc') as ascii;
+-------+
| ascii |
+-------+
| 97 |
+-------+
chr(int character_code)
返回数值ascii码对应的字符
--得到数值97对应的字符
[master:21000] > select chr(97) as chr;
+-----+
| chr |
+-----+
| a |
+-----+
btrim(string a)
去除字符串之前和之后的任意个数的空格
--去除hello前的空格
[master:21000] > select btrim(' hello ') as btrim;
+-------+
| btrim |
+-------+
| hello |
+-------+
btrim(string a,string chars_to_trim)
去除第⼀个字符串之前和之后的任何包含在第⼆个字符串中出现任意次数的字符(真的难理解QAQ)
--去除xyz并验证是否去除空格
[master:21000] > select btrim('xy hello zyzzxx','xyz') as btrim;
+------------+
| btrim |
+------------+
| hello |
+------------+
--验证是否会去除其他字符中间的应去除字符
[master:21000] > select btrim('xyhelxyzlozyzzxx','xyz') as btrim;
+----------+
| btrim |
+----------+
| helxyzlo |
+----------+
char_length(string a)
character_length(string a)
返回字符串的长度,两个函数功能相同
--char_length得到hello world的长度
[master:21000] > select char_length('hello world') as char_length;
+-------------+
| char_length |
+-------------+
| 11 |
+-------------+
--通过函数character_length得到hello world的长度
[master:21000] > select character_length('hello world') as character_length;
+------------------+
| character_length |
+------------------+
| 11 |
+------------------+
concat(string a,string b…)
拼接多个字符串
--连接hello和world两个字符串
[master:21000] > select concat('hello','world') as concat;
+------------+
| concat |
+------------+
| helloworld |
+------------+
--连接hello、world、cauchy三个字符串
[master:21000] > select concat('hello','world','cauchy') as concat;
+------------------+
| concat |
+------------------+
| helloworldcauchy |
+------------------+
concat_ws(string sep,string a,string b…)
拼接多个字符串,由指定分隔符分割
--通过'-'连接两个字符串
[master:21000] > select concat_ws('-','hello','world') as concat_ws;
+-------------+
| concat_ws |
+-------------+
| hello-world |
+-------------+
find_in_set(string str,string strList)
查某个字符串在⼀个以逗号为分隔符的列表中第⼀次出现的位置(以1为起点),如果查询不到或查询字符串中出现’,’(逗号),返回则为0
--在以逗号间隔的abcdefg中字符c第⼀次出现的位置
[master:21000] > select find_in_set('c','a,b,c,d,e,f,g') as find_in_set;
+-------------+
| find_in_set |
+-------------+
| 3 |
+-------------+
--在查询','的位置时的返回值
[master:21000] > select find_in_set(',','a,b,c,d,e,f,g') as find_in_set;
+-------------+
| find_in_set |
+-------------+
| 0 |
+-------------+
--在查询不存在字符的位置时的返回值
[master:21000] > select find_in_set('h','a,b,c,d,e,f,g') as find_in_set;
+-------------+
| find_in_set |
+-------------+
| 0 |
+-------------+
initcap(string str)
将字符串⾸字符⼤写并返回
--将'abc'⾸字母⼤写
[master:21000] > select initcap('abc') as initcap;
+---------+
| initcap |
+---------+
| Abc |
+---------+
instr(string str,string substr)
返回较长字符串中第⼀次出现⼦字符串的位置(从1开始)
--在字符串'abcdefg'中查'bcd'第⼀次出现的位置
[master:21000] > select instr('abcdefg','bcd') as instr;
+-------+
| instr |
+-------+
| 2 |
+-------+
length(string a)
返回参数字符串的字符长度
--得到字符串'abcdefg'的长度
[master:21000] > select length('abcdefg') as length;
+--------+
| length |
+--------+
| 7 |
+--------+
locate(string substr,string str,[int pos])
返回字符串中第⼀次出现⼦字符串的位置(从1开始),可指定位置
-
-返回长字符串中'bc'第⼀次出现的位置
[master:21000] > select locate('bc','abcdefgabc') as locate;
+--------+
| locate |
+--------+
| 2 |
+--------+
--返回长字符串中'bc'从第三位之后第⼀次出现的位置
[master:21000] > select locate('bc','abcdefgabc',3) as locate;
+--------+
| locate |
+--------+
| 9 |
+--------+
lower(string a)
lcase(string a)
返回全部为⼩写字符的字符串
--使⽤lower返回全⼩写的hello world
[master:21000] > select lower('Hello World') as lower;
+-------------+
| lower |
+-------------+
| hello world |
+-------------+
--使⽤lcase返回全⼩写的hello world
[master:21000] > select lcase('Hello World') as lcase;
+-------------+
| lcase |
+-------------+
| hello world |
upper(string a)
ucase(string a)
返回全部为⼤写字符的字符串
-
-使⽤upper返回全⼩写的hello worldregexp like
[master:21000] > select upper('hello world') as upper;
+-------------+
| upper |
+-------------+
| HELLO WORLD |
+-------------+
--使⽤ucase返回全⼩写的hello world
[master:21000] > select ucase('hello world') as ucase;
+-------------+
| ucase |
+-------------+
| HELLO WORLD |
+-------------+
lpad(string str,int len,string pad)
返回更改了长度的第⼀个字符串,如果⼩于长度,则⽤pad字符串在左边补齐,如果⼤于长度,则从左边截取对应长度字符串返回
--从左边截取长度为7的'hello world'
[master:21000] > select lpad('hello world',7,'/') as lpad;
+---------+
| lpad |
+---------+
| hello w |
+---------+
--从左边截取长度为13的'hello world',长度不⾜在左侧⽤'/'补齐
[master:21000] > select lpad('hello world',13,'/') as lpad;
+---------------+
| lpad |
+---------------+
| //hello world |
+---------------+
rpad(string str,int len,string pad)
返回更改了长度的第⼀个字符串,如果⼩于长度,则⽤pad字符串在右边补齐,如果⼤于长度,则从左边截取对应长度字符串返回
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论