赞
踩
hive> select length('abcedfg') from dual;
7
hive> select reverse(abcedfg’) from dual;
gfdecba
如有任何一个参数为null ,则返回值为 null。
select concat("my","name");
-- myname
select concat("my",null,"name");
-- null
如果分隔符为 null,则结果为 null,拼接的字符串为null,则会忽略该字符串。
select concat_ws("\t","my","name");
-- my name
select concat_ws(",","my",null,"name");
-- my,name
array<string>
对null会过滤
select collect_list(province) ,collect_set(province) from (select '北京' as province,'北京' as city union all select '河南' as province,'郑州' as city union all select '河南' as province,'郑州' as city union all select null as province,null as city )a ; collect_list(province) collect_set(province) ["河南","北京","河南"] ["河南","北京"] select province ,collect_list(city) ,collect_set(city) ,concat_ws('|',collect_set(city)) ,collect_set(concat_ws('|',province,city)) ,cast(collect_set(concat_ws('|',province,city)) as string) from (select '北京' as province,'北京' as city union all select '北京' as province,'北京' as city union all select '河南' as province,'郑州' as city union all select '河南' as province,'洛阳' as city union all select '河南' as province,'开封' as city )t group by province ; province collect_list(city) collect_set(city) concat_ws('|', collect_set(city)) collect_set(concat_ws('|', province,city)) cast(collect_set(concat_ws('|',province,city)) as string) 北京 ["北京","北京"] ["北京"] 北京 ["北京|北京"] [北京|北京] 河南 ["洛阳","郑州","开封"] ["开封","洛阳","郑州"] 开封|洛阳|郑州 ["河南|开封","河南|洛阳","河南|郑州"] [河南|开封, 河南|郑州, 河南|洛阳]
select substring("myname",0,2);
my
select upper("myname");
MYNAME
select lower("MYNAME");
myname
select trim(" myname ");
myname
select ltrim(" my name");
my name
select rtrim(" my name ");
my name
SELECT regexp_replace('100-200', '(\\d+)', 'num');
num-num
idx的数字不能大于表达式中()的个数,否则报错
如果没有,返回的是空字符串,而不是null
SELECT regexp_extract('100-200', '(\\d+)-(\\d+)', 1)
;--100
select regexp_extract('foothebar', 'foo(.*?)(bar)', 0)
; -- foothebar
select regexp_extract('foothebar', 'foo(.*?)(bar)', 1)
; -- the
select regexp_extract('foothebar', 'foo(.*?)(bar)', 2)
; -- bar
select regexp_extract('isStartDate=2019-07-14', '.*?StartDate\\=([^&]+)',1)
; --2019-07-14
SELECT parse_url('http://facebook.com/path/p1.php?query=1', 'HOST');
facebook.com
SELECT space(2);
两个空格
SELECT repeat('123', 2);
123123
SELECT ascii('31');
51返回3的asc码
SELECT lpad('hive2', 10, '1');
11111hive2
SELECT rpad('hive2', 10, '1');
ive211111
SELECT split('oneAtwoBthreeC', '[ABC]');
["one","two","three",""]
SELECT find_in_set('ab','abc,b,ab,c,def');
3
需要注意字段类型保持一致,若不一致则需要进行强制类型转换。
select split('[1,2,3,4]', '[\\[\\],]');
-- ["","1","2","3","4",""]
select array_contains(split('[1,2,3,4]', '[\\[\\],]'),'1');
-- true
select *
from login
where dt='20220101'
and (ver !='3.1' and ver !='3.2'
and ver != '4.0'
and ver != '5.2');
可以替换为
select *
from login
where dt='20220101'
and !array_contains(split('3.1,3.2,4.0,5.2',','),ver)
;
select stu_id , case when array_contains(collect_set(subject), '语文') or array_contains(collect_set(subject), '数学') or array_contains(collect_set(subject), '英语') then '基础' when array_contains(collect_set(subject), '科学') then '拓展' else '其他' end as subject_tag_name from student where dt = '20220101' group by stu_id
select instr("abcde",'b'); -- 2 select instr("abcdeabcde",'b'); -- 2 select instr("abcde",'f'); -- 0 select case when (instr("你是who呢", '你是') != 0 AND instr("你是who呢", '你是谁') = 0) then '包含:你是而不包含你是谁' end ; --网上有说可以 指定 搜索的开始位置,默认为1,测试不可以 select instr("abcdeabcde",'b',2,1); --Error while compiling statement: FAILED: SemanticException [Error 10015]: Line 2:7 Arguments length mismatch '1': The function INSTR accepts exactly 2 arguments.
select str_to_map("aaaa_-100#bbbb_领券29减8#cccc_29分钟",'#','_') as map_test
-- {"aaa":"领券29减8","bbb":"-100","ccc":"29分钟"}
-- 如果key一样,会取最后一个K-V对
select str_to_map("aaaa_-100#bbbb_领券29减8#cccc_29分钟#cccc_50分钟",'#','_') as map_test
-- {"aaa":"领券29减8","bbb":"-100","ccc":"50分钟"}
https://blog.csdn.net/weixin_43597208/article/details/135129633
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。