Impala String函數(shù)大全

Impala字符串函數(shù)

Impala中字符串函數(shù)主要應(yīng)用于 varchar、char、string類型,如果把varchar或者char類型的值傳遞給字符串函數(shù),返回將是一個(gè)string類型的值

函數(shù)列表

base64encode(string str)
base64decode(string str)

加密和解密,返回值為4字節(jié)的倍數(shù),可以用來(lái)存儲(chǔ)特殊字符串

--將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)

返回參數(shù)字符串的第一個(gè)字符的ascii碼

--得到字符a的ascii碼
[master:21000] > select ascii('a') as ascii;
+-------+
| ascii |
+-------+
| 97    |
+-------+
--驗(yàn)證是否只能返回第一個(gè)字符
[master:21000] > select ascii('abc') as ascii;
+-------+
| ascii |
+-------+
| 97    |
+-------+

chr(int character_code)

返回?cái)?shù)值ascii碼對(duì)應(yīng)的字符

--得到數(shù)值97對(duì)應(yīng)的字符
[master:21000] > select chr(97) as chr;
+-----+
| chr |
+-----+
| a   |
+-----+

btrim(string a)

去除字符串之前和之后的任意個(gè)數(shù)的空格

--去除hello前的空格
[master:21000] > select btrim('    hello ') as btrim;
+-------+
| btrim |
+-------+
| hello |
+-------+

btrim(string a,string chars_to_trim)

去除第一個(gè)字符串之前和之后的任何包含在第二個(gè)字符串中出現(xiàn)任意次數(shù)的字符(真的難理解QAQ)

--去除xyz并驗(yàn)證是否去除空格
[master:21000] > select btrim('xy    hello zyzzxx','xyz') as btrim;
+------------+
| btrim      |
+------------+
|     hello  |
+------------+
--驗(yàn)證是否會(huì)去除其他字符中間的應(yīng)去除字符
[master:21000] > select btrim('xyhelxyzlozyzzxx','xyz') as btrim;
+----------+
| btrim    |
+----------+
| helxyzlo |
+----------+

char_length(string a)

character_length(string a)

返回字符串的長(zhǎng)度,兩個(gè)函數(shù)功能相同

--char_length得到hello world的長(zhǎng)度
[master:21000] > select char_length('hello world') as char_length;
+-------------+
| char_length |
+-------------+
| 11          |
+-------------+
--通過(guò)函數(shù)character_length得到hello world的長(zhǎng)度
[master:21000] > select character_length('hello world') as character_length;
+------------------+
| character_length |
+------------------+
| 11               |
+------------------+

concat(string a,string b...)

拼接多個(gè)字符串

--連接hello和world兩個(gè)字符串
[master:21000] > select concat('hello','world') as concat;
+------------+
| concat     |
+------------+
| helloworld |
+------------+
--連接hello、world、cauchy三個(gè)字符串
[master:21000] > select concat('hello','world','cauchy') as concat;
+------------------+
| concat           |
+------------------+
| helloworldcauchy |
+------------------+

concat_ws(string sep,string a,string b...)

拼接多個(gè)字符串,由指定分隔符分割

--通過(guò)'-'連接兩個(gè)字符串
[master:21000] > select concat_ws('-','hello','world') as concat_ws;
+-------------+
| concat_ws   |
+-------------+
| hello-world |
+-------------+

find_in_set(string str,string strList)

查找某個(gè)字符串在一個(gè)以逗號(hào)為分隔符的列表中第一次出現(xiàn)的位置(以1為起點(diǎn)),如果查詢不到或查詢字符串中出現(xiàn)','(逗號(hào)),返回則為0

--在以逗號(hào)間隔的abcdefg中字符c第一次出現(xiàn)的位置
[master:21000] > select find_in_set('c','a,b,c,d,e,f,g') as find_in_set;
+-------------+
| find_in_set |
+-------------+
| 3           |
+-------------+
--在查詢','的位置時(shí)的返回值
[master:21000] > select find_in_set(',','a,b,c,d,e,f,g') as find_in_set;
+-------------+
| find_in_set |
+-------------+
| 0           |
+-------------+
--在查詢不存在字符的位置時(shí)的返回值
[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)

返回較長(zhǎng)字符串中第一次出現(xiàn)子字符串的位置(從1開(kāi)始)

--在字符串'abcdefg'中查找'bcd'第一次出現(xiàn)的位置
[master:21000] > select instr('abcdefg','bcd') as instr;
+-------+
| instr |
+-------+
| 2     |
+-------+

length(string a)

返回參數(shù)字符串的字符長(zhǎng)度

--得到字符串'abcdefg'的長(zhǎng)度
[master:21000] > select length('abcdefg') as length;
+--------+
| length |
+--------+
| 7      |
+--------+

locate(string substr,string str,[int pos])

返回字符串中第一次出現(xiàn)子字符串的位置(從1開(kāi)始),可指定位置

--返回長(zhǎng)字符串中'bc'第一次出現(xiàn)的位置
[master:21000] > select locate('bc','abcdefgabc') as locate;
+--------+
| locate |
+--------+
| 2      |
+--------+
--返回長(zhǎng)字符串中'bc'從第三位之后第一次出現(xiàn)的位置
[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 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 |
+-------------+

lpad(string str,int len,string pad)

返回更改了長(zhǎng)度的第一個(gè)字符串,如果小于長(zhǎng)度,則用pad字符串在左邊補(bǔ)齊,如果大于長(zhǎng)度,則從左邊截取對(duì)應(yīng)長(zhǎng)度字符串返回

--從左邊截取長(zhǎng)度為7的'hello world'
[master:21000] > select lpad('hello world',7,'/') as lpad;
+---------+
| lpad    |
+---------+
| hello w |
+---------+
--從左邊截取長(zhǎng)度為13的'hello world',長(zhǎng)度不足在左側(cè)用'/'補(bǔ)齊
[master:21000] > select lpad('hello world',13,'/') as lpad;
+---------------+
| lpad          |
+---------------+
| //hello world |
+---------------+

rpad(string str,int len,string pad)

返回更改了長(zhǎng)度的第一個(gè)字符串,如果小于長(zhǎng)度,則用pad字符串在右邊補(bǔ)齊,如果大于長(zhǎng)度,則從左邊截取對(duì)應(yīng)長(zhǎng)度字符串返回

--從左邊截取長(zhǎng)度為7的'hello world'
[master:21000] > select rpad('hello world',7,'/') as rpad;
+---------+
| rpad    |
+---------+
| hello w |
+---------+
--從左邊截取長(zhǎng)度為13的'hello world',長(zhǎng)度不足在右側(cè)用'/'補(bǔ)齊
[master:21000] > select rpad('hello world',13,'/') as rpad;
+---------------+
| rpad          |
+---------------+
| hello world// |
+---------------+

ltrim(string a)

返回參數(shù)字符串,并從左側(cè)刪除任何前導(dǎo)空格

--刪除字符串'  hello  '左側(cè)的所有空格
[master:21000] > select ltrim('  hello  ') as ltrim;
+---------+
| ltrim   |
+---------+
| hello   |
+---------+

rtrim(string a)

返回參數(shù)字符串,并從右側(cè)刪除任何后置空格

--刪除字符串'  hello  '右側(cè)的所有空格
[master:21000] > select rtrim('  hello  ') as rtrim;
+---------+
| rtrim   |
+---------+
|   hello |
+---------+

trim(string a)

去掉字符串中所有前導(dǎo)和后置空格

--去掉'  hello world  '的前導(dǎo)和后置空格
[master:21000] > select trim('  hello world  ') as trim;
+-------------+
| trim        |
+-------------+
| hello world |
+-------------+

regexp_extract(string subject,string pattern,int index)

返回通過(guò)正則表達(dá)式提取的字符串,
impala使用\字符進(jìn)行轉(zhuǎn)義,所以\d需要\d,也可以采用[[:digit:]]

--匹配任意字符以數(shù)字結(jié)尾,返回匹配的整個(gè)字符串
[master:21000] >  select regexp_extract('abcdef123ghi456jkl','.*?(\\d+)',0);
+------------------------------------------------------+
| regexp_extract('abcdef123ghi456jkl', '.*?(\\d+)', 0) |
+------------------------------------------------------+
| abcdef123ghi456                                      |
+------------------------------------------------------+
--匹配任意字符以數(shù)字結(jié)尾,只返回匹配的第一個(gè)值
[master:21000] > select regexp_extract('abcdef123ghi456jkl','.*?(\\d+)',1);
+------------------------------------------------------+
| regexp_extract('abcdef123ghi456jkl', '.*?(\\d+)', 1) |
+------------------------------------------------------+
| 456                                                  |
+------------------------------------------------------+
--匹配任意字符以小寫字母結(jié)尾,返回匹配的整個(gè)字符串
[master:21000] > select regexp_extract('AbcdBCdefGHI','.*?([[:lower:]]+)',0);
+--------------------------------------------------------+
| regexp_extract('abcdbcdefghi', '.*?([[:lower:]]+)', 0) |
+--------------------------------------------------------+
| AbcdBCdef                                              |
+--------------------------------------------------------+
--匹配任意字符以小寫字母結(jié)尾,只返回匹配的第一個(gè)值
[master:21000] > select regexp_extract('AbcdBCdefGHI','.*?([[:lower:]]+)',1);
+--------------------------------------------------------+
| regexp_extract('abcdbcdefghi', '.*?([[:lower:]]+)', 1) |
+--------------------------------------------------------+
| def                                                    |
+--------------------------------------------------------+

regexp_like(string source,string pattern,[string options])

返回true或者false,表示字符串是否包含正則表達(dá)式的值
options參數(shù):

  • c: 區(qū)分大小寫匹配(默認(rèn))
  • i:不區(qū)分大小寫
  • 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',設(shè)置參數(shù)不區(qū)分大小寫
[master:21000] > select regexp_like('foo','F','i');
+------------------------------+
| regexp_like('foo', 'f', 'i') |
+------------------------------+
| true                         |
+------------------------------+

regexp_replace(string initial,string pattern,string replacement)

替換字符串與正則表達(dá)式匹配項(xiàng)為新字符串并返回

--將字符串中任意的字符'b'替換為'xyz'
[master:21000] > select regexp_replace('aaabbbaaa','b+','xyz');
+------------------------------------------+
| regexp_replace('aaabbbaaa', 'b+', 'xyz') |
+------------------------------------------+
| aaaxyzaaa                                |
+------------------------------------------+
--將字符串中任意的非數(shù)字字符替換為''(空)
[master:21000] > select regexp_replace('123-456-789','[^[:digit:]]','');
+---------------------------------------------------+
| regexp_replace('123-456-789', '[^[:digit:]]', '') |
+---------------------------------------------------+
| 123456789                                         |
+---------------------------------------------------+

repeat(string str,int n)

返回指定重復(fù)次數(shù)的字符串

--將'hello'重復(fù)5次
[master:21000] > select repeat('hello',5) as repeat;
+---------------------------+
| repeat                    |
+---------------------------+
| hellohellohellohellohello |
+---------------------------+

reverse(string a)

返回反轉(zhuǎn)字符串

--反轉(zhuǎn)字符串'hello world'
[master:21000] > select reverse('hello world') as reverse;
+-------------+
| reverse     |
+-------------+
| dlrow olleh |
+-------------+

space(int n)

返回指定數(shù)量的空格的連接字符串

--返回5個(gè)連續(xù)空格的字符串
[master:21000] > select space(5) as space;
+-------+
| space |
+-------+
|       |
+-------+

split_part(string source,string delimiter,bigint n)

以delimiter字符串作為拆分項(xiàng),取第n個(gè)字符串返回

--以','為分隔符拆分'x,y,z'并返回第1個(gè)字符串
[master:21000] > select split_part('x,y,z',',',1);
+-----------------------------+
| split_part('x,y,z', ',', 1) |
+-----------------------------+
| x                           |
+-----------------------------+
--以','為分隔符拆分'x,y,z'并返回第2個(gè)字符串
[master:21000] > select split_part('x,y,z',',',2);
+-----------------------------+
| split_part('x,y,z', ',', 2) |
+-----------------------------+
| y                           |
+-----------------------------+
--以','為分隔符拆分'x,y,z'并返回第3個(gè)字符串
[master:21000] > select split_part('x,y,z',',',3);
+-----------------------------+
| split_part('x,y,z', ',', 3) |
+-----------------------------+
| z                           |
+-----------------------------+

strleft(string a,int num_chars)

截取字符串,返回左邊的n個(gè)字符

--從左邊截取字符串'hello world',返回長(zhǎng)度為4的字符串
[master:21000] > select strleft('hello world',4) as strleft;
+---------+
| strleft |
+---------+
| hell    |
+---------+

strright(string a,int num_chars)

截取字符串,返回右邊的n個(gè)字符

--從右邊截取字符串'hello world',返回長(zhǎng)度為4的字符串
[master:21000] > select strright('hello world',4) as strright;
+----------+
| strright |
+----------+
| orld     |
+----------+

substr(string a,int start,[int len])

substring(string a,int start,[int len])

返回從指定點(diǎn)開(kāi)始的字符串部分,可選地指定最大長(zhǎng)度

--截取字符串'hello world',從第6位開(kāi)始
[master:21000] > select substr('hello world',6) as substr;
+--------+
| substr |
+--------+
|  world |
+--------+
--截取字符串'hello world',從第6位開(kāi)始,長(zhǎng)度為3
[master:21000] > select substr('hello world',6,3) as substr;
+--------+
| substr |
+--------+
|  wo    |
+--------+
--截取字符串'hello world',從第6位開(kāi)始
[master:21000] > select substring('hello world',6) as substring;
+-----------+
| substring |
+-----------+
|  world    |
+-----------+
--截取字符串'hello world',從第6位開(kāi)始,長(zhǎng)度為3
[master:21000] > select substring('hello world',6,3) as substring;
+-----------+
| substring |
+-----------+
|  wo       |
+-----------+

translate(string input,string from,string to)

將字符串中的一些字符替換為其他字符

不能替換字符串,from字符串與to字符串一一對(duì)應(yīng),再替換 input字符串中所有對(duì)應(yīng)字符

--將'world'替換為'cauchy',只能匹配到想相同長(zhǎng)度,即'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 |
+-------------+
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 在C語(yǔ)言中,五種基本數(shù)據(jù)類型存儲(chǔ)空間長(zhǎng)度的排列順序是: A)char B)char=int<=float C)ch...
    夏天再來(lái)閱讀 4,006評(píng)論 0 2
  • 標(biāo)準(zhǔn)c++中string類函數(shù)介紹 注意不是CString 之所以拋棄char*的字符串而選用C++標(biāo)準(zhǔn)程序庫(kù)中的...
    杰倫哎呦哎呦閱讀 1,377評(píng)論 0 8
  • Hive函數(shù) Hive函數(shù) 一、關(guān)系運(yùn)算: 等值比較: = 語(yǔ)法:A=B操作類型:所有基本類型描述:如果表達(dá)式A與...
    依天立業(yè)閱讀 1,129評(píng)論 0 8
  • 獨(dú)坐歲月,一季喧囂里,守著一簾幽夢(mèng),走走停停的站臺(tái)上,感動(dòng)著一次次遇見(jiàn),或深或淺,倍感溫暖,動(dòng)蕩的站臺(tái),起伏的心音...
    楓淚_22e5閱讀 493評(píng)論 0 0
  • mochow閱讀 437評(píng)論 0 2

友情鏈接更多精彩內(nèi)容