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 |
+-------------+

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

返回更改了长度的第一个字符串,如果小于长度,则用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字符串在右边补齐,如果大于长度,则从左边截取对应长度字符串返回

--从左边截取长度为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// |
+---------------+

ltrim(string a)

返回参数字符串,并从左侧删除任何前导空格

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

rtrim(string a)

返回参数字符串,并从右侧删除任何后置空格

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

trim(string a)

去掉字符串中所有前导和后置空格

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

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                                                    |
+--------------------------------------------------------+

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                         |
+------------------------------+

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                                         |
+---------------------------------------------------+

repeat(string str,int n)

返回指定重复次数的字符串

--将'hello'重复5次
[master:21000] > select repeat('hello',5) as repeat;
+---------------------------+
| repeat                    |
+---------------------------+
| hellohellohellohellohello |
+---------------------------+

reverse(string a)

返回反转字符串

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

space(int n)

返回指定数量的空格的连接字符串

--返回5个连续空格的字符串
[master:21000] > select space(5) as space;
+-------+
| space |
+-------+
|       |
+-------+

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                           |
+-----------------------------+

strleft(string a,int num_chars)

截取字符串,返回左边的n个字符

--从左边截取字符串'hello world',返回长度为4的字符串
[master:21000] > select strleft('hello world',4) as strleft;
+---------+
| strleft |
+---------+
| hell    |
+---------+

strright(string a,int num_chars)

截取字符串,返回右边的n个字符

--从右边截取字符串'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])

返回从指定点开始的字符串部分,可选地指定最大长度

--截取字符串'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字符串一一对应,再替换 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 |
+-------------+
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,519评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,842评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,544评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,742评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,646评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,027评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,513评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,169评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,324评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,268评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,299评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,996评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,591评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,667评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,911评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,288评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,871评论 2 341

推荐阅读更多精彩内容

  • 在C语言中,五种基本数据类型存储空间长度的排列顺序是: A)char B)char=int<=float C)ch...
    夏天再来阅读 3,307评论 0 2
  • 标准c++中string类函数介绍 注意不是CString 之所以抛弃char*的字符串而选用C++标准程序库中的...
    杰伦哎呦哎呦阅读 1,314评论 0 8
  • Hive函数 Hive函数 一、关系运算: 等值比较: = 语法:A=B操作类型:所有基本类型描述:如果表达式A与...
    依天立业阅读 809评论 0 8
  • 独坐岁月,一季喧嚣里,守着一帘幽梦,走走停停的站台上,感动着一次次遇见,或深或浅,倍感温暖,动荡的站台,起伏的心音...
    枫泪_22e5阅读 403评论 0 0
  • mochow阅读 395评论 0 2