-- 查看说明
desc function extended 函数名
/**/。
-- 返回时间戳
select unix_timestamp();
select unix_timestamp('2019-01-01 01:01:01');
select unix_timestamp('20190101 010101','yyyyMMdd HHmmss');
-- 时间戳转时间
select from_unixtime(1546304461,'yyyy-MM-dd HH:mm:ss');
-- 返回当前日期
select current_date;
-- 返回日期部分
select to_date('2020-02-03 04:05:06');
-- 获取年、月、日、时、分、秒
select year('2020-02-03 04:05:06');
select month('2020-02-03 04:05:06');
select day('2020-02-03 04:05:06');
select hour('2020-02-03 04:05:06');
select minute('2020-02-03 04:05:06');
select second('2020-02-03 04:05:06');
-- 返回当年第几周【最后一周算明年】
select weekofyear('2020-01-01');
select weekofyear('2020-02-03 04:05:06');
-- 返回当月第几天
select dayofmonth('2020-02-03');
select dayofmonth('2020-02-03 04:05:06');
-- 返回两个日期间的月份 前-后 返回小数
select months_between('2020-02-03 04:05:06','2020-03-05 04:05:06');
-- 月加减
select add_months('2020-02-03',1);
select add_months('2020-02-03',-1);
-- 日期相差天数 前-后
select datediff('2020-02-03 04:05:06','2020-03-05 04:05:06');
-- 日期加减
select date_add('2020-02-03',1);
select date_add('2020-02-03',-1);
select date_sub('2020-02-03',1);
-- 返回当月最后一天
select last_day('2020-02-03');
/*
取整函数
*/
-- 四舍五入
select round(1.5);
select round(1.4);
select round(1.445,2);
-- 向上取整
select ceil(1.1);
-- 向下取整
select floor(1.9);
/*
字符串相关
*/
-- 大小写转换
select uppper('abc');
select lower('ABC');
-- 长度
select length('abc');
-- 去除前后空格
select trim(' abc ');
-- 向作/右补齐指定长度
select lpad('abc',5,'1');
select rpad('abc',5,'1');
-- 正则替换 参数1 要匹配的字符串,参数2 正则表达式,参数3 替换字符
select regexp_replace('hello world123','(\\d+)','!');
-- 截取字符串
select substring('abcdefg',5,1);
/*
集合相关操作
*/
-- 集合中的元素
select size(array('b','c','d'));
-- 返回map中的key\value
select map_keys();
select map_values();
-- 判断集合中是否包含某个元素 返回boolean
select array_contains(array('a','b','c'),'a');
-- 将array中的元素排序
select sort_array(array('c','b','a'));
/*
其他常用函数【重点】
*/
-- nvl函数 判断参数1是否为null,为null返回 参数2的值
select nvl(null,'bla');
-- 字符串拼接 有一个参数为null 返回null
select concat('a','b','c');
select concat('a',null,'c');
-- 字符串拼接指定分割 ,可传入数组
select concat_ws('.','a',array('b','c','d'));
-- 判断函数
select if(true,0,1);
-- 判断函数2 相当于decode
select case 1 when 1 then 'a' else 'b' end;
select case 1 when 2 then 'a' else 'b' end;
Hive函数Demo1:基础常用函数
最新推荐文章于 2022-07-15 23:51:29 发布