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ù)能真,可以用來存儲(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碼對應(yīng)的字符

--得到數(shù)值97對應(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)

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

--char_length得到hello world的長度
[master:21000] > select char_length('hello world') as char_length;
+-------------+
| char_length |
+-------------+
| 11          |
+-------------+
--通過函數(shù)character_length得到hello world的長度
[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è)字符串疼约,由指定分隔符分割

--通過'-'連接兩個(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)

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

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

length(string a)

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

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

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

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

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

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

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

rpad(string str,int len,string pad)

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

--從左邊截取長度為7的'hello world'
[master:21000] > select rpad('hello world',7,'/') as rpad;
+---------+
| rpad    |
+---------+
| hello w |
+---------+
--從左邊截取長度為13的'hello world',長度不足在右側(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)

返回通過正則表達(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'拘荡,返回長度為4的字符串
[master:21000] > select strleft('hello world',4) as strleft;
+---------+
| strleft |
+---------+
| hell    |
+---------+

strright(string a,int num_chars)

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

--從右邊截取字符串'hello world',返回長度為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)開始的字符串部分,可選地指定最大長度

--截取字符串'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       |
+-----------+

translate(string input,string from,string to)

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

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

--將'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 |
+-------------+
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末远寸,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子屠凶,更是在濱河造成了極大的恐慌驰后,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,548評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件阅畴,死亡現(xiàn)場離奇詭異倡怎,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)贱枣,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,497評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來颤专,“玉大人纽哥,你說我怎么就攤上這事∑茱酰” “怎么了春塌?”我有些...
    開封第一講書人閱讀 167,990評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長簇捍。 經(jīng)常有香客問我只壳,道長,這世上最難降的妖魔是什么暑塑? 我笑而不...
    開封第一講書人閱讀 59,618評(píng)論 1 296
  • 正文 為了忘掉前任吼句,我火速辦了婚禮,結(jié)果婚禮上事格,老公的妹妹穿的比我還像新娘惕艳。我一直安慰自己,他們只是感情好驹愚,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,618評(píng)論 6 397
  • 文/花漫 我一把揭開白布远搪。 她就那樣靜靜地躺著,像睡著了一般逢捺。 火紅的嫁衣襯著肌膚如雪谁鳍。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,246評(píng)論 1 308
  • 那天,我揣著相機(jī)與錄音倘潜,去河邊找鬼余佛。 笑死,一個(gè)胖子當(dāng)著我的面吹牛窍荧,可吹牛的內(nèi)容都是我干的辉巡。 我是一名探鬼主播,決...
    沈念sama閱讀 40,819評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼蕊退,長吁一口氣:“原來是場噩夢啊……” “哼郊楣!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起瓤荔,我...
    開封第一講書人閱讀 39,725評(píng)論 0 276
  • 序言:老撾萬榮一對情侶失蹤净蚤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后输硝,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體今瀑,經(jīng)...
    沈念sama閱讀 46,268評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,356評(píng)論 3 340
  • 正文 我和宋清朗相戀三年点把,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了橘荠。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,488評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡郎逃,死狀恐怖哥童,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情褒翰,我是刑警寧澤贮懈,帶...
    沈念sama閱讀 36,181評(píng)論 5 350
  • 正文 年R本政府宣布,位于F島的核電站优训,受9級(jí)特大地震影響朵你,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜揣非,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,862評(píng)論 3 333
  • 文/蒙蒙 一抡医、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧妆兑,春花似錦魂拦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,331評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至腺逛,卻和暖如春荷愕,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,445評(píng)論 1 272
  • 我被黑心中介騙來泰國打工安疗, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留抛杨,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,897評(píng)論 3 376
  • 正文 我出身青樓荐类,卻偏偏與公主長得像怖现,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子玉罐,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,500評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容

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