Impala String函數大全


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 |
+-------------+
1
2
3
4
5
6
7
8
9
10
11
12
13
14
ascii(string str)
返回參數字符串的第一個字符的ascii碼

--得到字符a的ascii碼
[master:21000] > select ascii('a') as ascii;
+-------+
| ascii |
+-------+
| 97 |
+-------+
--驗證是否只能返回第一個字符
[master:21000] > select ascii('abc') as ascii;
+-------+
| ascii |
+-------+
| 97 |
+-------+
1
2
3
4
5
6
7
8
9
10
11
12
13
14
chr(int character_code)
返回數值ascii碼對應的字符

--得到數值97對應的字符
[master:21000] > select chr(97) as chr;
+-----+
| chr |
+-----+
| a |
+-----+
1
2
3
4
5
6
7
btrim(string a)
去除字符串之前和之后的任意個數的空格

--去除hello前的空格
[master:21000] > select btrim(' hello ') as btrim;
+-------+
| btrim |
+-------+
| hello |
+-------+
1
2
3
4
5
6
7
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 |
+----------+
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 |
+------------------+
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 |
+------------------+
1
2
3
4
5
6
7
8
9
10
11
12
13
14
concat_ws(string sep,string a,string b…)
拼接多個字符串,由指定分隔符分割

--通過'-'連接兩個字符串
[master:21000] > select concat_ws('-','hello','world') as concat_ws;
+-------------+
| concat_ws |
+-------------+
| hello-world |
+-------------+
1
2
3
4
5
6
7
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 |
+-------------+
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
initcap(string str)
將字符串首字符大寫並返回

--將'abc'首字母大寫
[master:21000] > select initcap('abc') as initcap;
+---------+
| initcap |
+---------+
| Abc |
+---------+
1
2
3
4
5
6
7
instr(string str,string substr)
返回較長字符串中第一次出現子字符串的位置(從1開始)

--在字符串'abcdefg'中查找'bcd'第一次出現的位置
[master:21000] > select instr('abcdefg','bcd') as instr;
+-------+
| instr |
+-------+
| 2 |
+-------+
1
2
3
4
5
6
7
length(string a)
返回參數字符串的字符長度

--得到字符串'abcdefg'的長度
[master:21000] > select length('abcdefg') as length;
+--------+
| length |
+--------+
| 7 |
+--------+
1
2
3
4
5
6
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 |
+--------+
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 |
1
2
3
4
5
6
7
8
9
10
11
12
13
upper(string a)
ucase(string a)
返回全部為大寫字符的字符串

--使用upper返回全小寫的hello world
[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 |
+-------------+
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 |
+---------------+
1
2
3
4
5
6
7
8
9
10
11
12
13
14
rpad(string str,int len,string pad)
返回更改了長度的第一個字符串,如果小於長度,則用pad字符串在右邊補齊,如果大於長度,則從左邊截取對應長度字符串返回

--從左邊截取長度為7的'hello world'
[master:21000] > select rpad('hello world',7,'/') as rpad;
+---------+
| rpad |
+---------+
| hello w |
+---------+
--從左邊截取長度為13的'hello world',長度不足在右側用'/'補齊
[master:21000] > select rpad('hello world',13,'/') as rpad;
+---------------+
| rpad |
+---------------+
| hello world// |
+---------------+
1
2
3
4
5
6
7
8
9
10
11
12
13
14
ltrim(string a)
返回參數字符串,並從左側刪除任何前導空格

--刪除字符串' hello '左側的所有空格
[master:21000] > select ltrim(' hello ') as ltrim;
+---------+
| ltrim |
+---------+
| hello |
+---------+
1
2
3
4
5
6
7
rtrim(string a)
返回參數字符串,並從右側刪除任何后置空格

--刪除字符串' hello '右側的所有空格
[master:21000] > select rtrim(' hello ') as rtrim;
+---------+
| rtrim |
+---------+
| hello |
+---------+
1
2
3
4
5
6
7
trim(string a)
去掉字符串中所有前導和后置空格

--去掉' hello world '的前導和后置空格
[master:21000] > select trim(' hello world ') as trim;
+-------------+
| trim |
+-------------+
| hello world |
+-------------+
1
2
3
4
5
6
7
regexp_extract(string subject,string pattern,int index)
返回通過正則表達式提取的字符串,
impala使用\字符進行轉義,所以\d需要\d,也可以采用[[:digit:]]

--匹配任意字符以數字結尾,返回匹配的整個字符串
[master:21000] > select regexp_extract('abcdef123ghi456jkl','.*?(\\d+)',0);
+------------------------------------------------------+
| regexp_extract('abcdef123ghi456jkl', '.*?(\\d+)', 0) |
+------------------------------------------------------+
| abcdef123ghi456 |
+------------------------------------------------------+
--匹配任意字符以數字結尾,只返回匹配的第一個值
[master:21000] > select regexp_extract('abcdef123ghi456jkl','.*?(\\d+)',1);
+------------------------------------------------------+
| regexp_extract('abcdef123ghi456jkl', '.*?(\\d+)', 1) |
+------------------------------------------------------+
| 456 |
+------------------------------------------------------+
--匹配任意字符以小寫字母結尾,返回匹配的整個字符串
[master:21000] > select regexp_extract('AbcdBCdefGHI','.*?([[:lower:]]+)',0);
+--------------------------------------------------------+
| regexp_extract('abcdbcdefghi', '.*?([[:lower:]]+)', 0) |
+--------------------------------------------------------+
| AbcdBCdef |
+--------------------------------------------------------+
--匹配任意字符以小寫字母結尾,只返回匹配的第一個值
[master:21000] > select regexp_extract('AbcdBCdefGHI','.*?([[:lower:]]+)',1);
+--------------------------------------------------------+
| regexp_extract('abcdbcdefghi', '.*?([[:lower:]]+)', 1) |
+--------------------------------------------------------+
| def |
+--------------------------------------------------------+
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
regexp_like(string source,string pattern,[string options])
返回true或者false,表示字符串是否包含正則表達式的值
options參數:
- c: 區分大小寫匹配(默認)
- i:不區分大小寫
- m:多行匹配
- n:換行符匹配

--判斷字符'foo'是否包含'f'
[master:21000] > select regexp_like('foo','f');
+-------------------------+
| regexp_like('foo', 'f') |
+-------------------------+
| true |
+-------------------------+
--判斷字符'foo'是否包含'F'
[master:21000] > select regexp_like('foo','F');
+-------------------------+
| regexp_like('foo', 'f') |
+-------------------------+
| false |
+-------------------------+
--判斷字符'foo'是否包含'f',設置參數不區分大小寫
[master:21000] > select regexp_like('foo','F','i');
+------------------------------+
| regexp_like('foo', 'f', 'i') |
+------------------------------+
| true |
+------------------------------+
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
regexp_replace(string initial,string pattern,string replacement)
替換字符串與正則表達式匹配項為新字符串並返回

--將字符串中任意的字符'b'替換為'xyz'
[master:21000] > select regexp_replace('aaabbbaaa','b+','xyz');
+------------------------------------------+
| regexp_replace('aaabbbaaa', 'b+', 'xyz') |
+------------------------------------------+
| aaaxyzaaa |
+------------------------------------------+
--將字符串中任意的非數字字符替換為''(空)
[master:21000] > select regexp_replace('123-456-789','[^[:digit:]]','');
+---------------------------------------------------+
| regexp_replace('123-456-789', '[^[:digit:]]', '') |
+---------------------------------------------------+
| 123456789 |
+---------------------------------------------------+
1
2
3
4
5
6
7
8
9
10
11
12
13
14
repeat(string str,int n)
返回指定重復次數的字符串

--將'hello'重復5次
[master:21000] > select repeat('hello',5) as repeat;
+---------------------------+
| repeat |
+---------------------------+
| hellohellohellohellohello |
+---------------------------+
1
2
3
4
5
6
7
reverse(string a)
返回反轉字符串

--反轉字符串'hello world'
[master:21000] > select reverse('hello world') as reverse;
+-------------+
| reverse |
+-------------+
| dlrow olleh |
+-------------+
1
2
3
4
5
6
7
space(int n)
返回指定數量的空格的連接字符串

--返回5個連續空格的字符串
[master:21000] > select space(5) as space;
+-------+
| space |
+-------+
| |
+-------+
1
2
3
4
5
6
7
split_part(string source,string delimiter,bigint n)
以delimiter字符串作為拆分項,取第n個字符串返回

--以','為分隔符拆分'x,y,z'並返回第1個字符串
[master:21000] > select split_part('x,y,z',',',1);
+-----------------------------+
| split_part('x,y,z', ',', 1) |
+-----------------------------+
| x |
+-----------------------------+
--以','為分隔符拆分'x,y,z'並返回第2個字符串
[master:21000] > select split_part('x,y,z',',',2);
+-----------------------------+
| split_part('x,y,z', ',', 2) |
+-----------------------------+
| y |
+-----------------------------+
--以','為分隔符拆分'x,y,z'並返回第3個字符串
[master:21000] > select split_part('x,y,z',',',3);
+-----------------------------+
| split_part('x,y,z', ',', 3) |
+-----------------------------+
| z |
+-----------------------------+
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
strleft(string a,int num_chars)
截取字符串,返回左邊的n個字符

--從左邊截取字符串'hello world',返回長度為4的字符串
[master:21000] > select strleft('hello world',4) as strleft;
+---------+
| strleft |
+---------+
| hell |
+---------+
1
2
3
4
5
6
7
strright(string a,int num_chars)
截取字符串,返回右邊的n個字符

--從右邊截取字符串'hello world',返回長度為4的字符串
[master:21000] > select strright('hello world',4) as strright;
+----------+
| strright |
+----------+
| orld |
+----------+
1
2
3
4
5
6
7
substr(string a,int start,[int len])
substring(string a,int start,[int len])
返回從指定點開始的字符串部分,可選地指定最大長度

--截取字符串'hello world',從第6位開始
[master:21000] > select substr('hello world',6) as substr;
+--------+
| substr |
+--------+
| world |
+--------+
--截取字符串'hello world',從第6位開始,長度為3
[master:21000] > select substr('hello world',6,3) as substr;
+--------+
| substr |
+--------+
| wo |
+--------+
--截取字符串'hello world',從第6位開始
[master:21000] > select substring('hello world',6) as substring;
+-----------+
| substring |
+-----------+
| world |
+-----------+
--截取字符串'hello world',從第6位開始,長度為3
[master:21000] > select substring('hello world',6,3) as substring;
+-----------+
| substring |
+-----------+
| wo |
+-----------+
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
translate(string input,string from,string to)
將字符串中的一些字符替換為其他字符
注:不能替換字符串,from字符串與to字符串一一對應,再替換 input字符串中所有對應字符

--將'world'替換為'cauchy',只能匹配到想相同長度,即'cauch',且拆分為w->c,o->a,r->u,l->c,d->h
[master:21000] > select translate('hello world','world','cauchy') as translate;
+-------------+
| translate |
+-------------+
| hecca cauch |
+-------------+
--替換字符串中所有屬於'world'的字符為'abcde'
[master:21000] > select translate('hello world','world','abcde') as translate;
+-------------+
| translate |
+-------------+
| heddb abcde |
+-------------+
————————————————
版權聲明:本文為CSDN博主「柯西Cauchy」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/qq_24699959/article/details/79863664


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM