时间函数
# 按照指定时间格式获取当前时间
select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:mm:ss') ;
# 时间戳转日期
select from_unixtime(1505456567);
select from_unixtime(1505456567,'yyyyMMdd');
select from_unixtime(1505456567,'yyyy-MM-dd HH:mm:ss');
# 获取当前时间戳
select unix_timestamp();
# 日期转时间戳
select unix_timestamp('2017-09-15 14:23:00');
# 计算时间差
select datediff('2018-06-18','2018-11-21');
# 查询当月第几天
select dayofmonth(current_date);
#月末:
select last_day(current_date)
#当月第1天:
select date_sub(current_date,dayofmonth(current_date)-1)
#下个月第1天:
select add_months(date_sub(current_date,dayofmonth(current_date)-1),1)
# 当前日期
select current_date
数学函数
- round 四舍五入((42.3 =>42))
- ceil 向上取整(42.3 =>43)
- floor 向下取整(42.3 =>42)
字符串函数
- lower(转小写)
- upper(转大写)
- length(字符串长度,字符数)
- concat(字符串拼接)
- concat_ws (指定分隔符)
- substr(求子串)
# 指定长度求子串
substr(string A, int start)
- split (字符串分割函数)
split('a,b,c,d',',')
["a","b","c","d"]
split('a,b,c,d',',')[0]
a
# 注意特殊字符需要转义
split('192.168.0.1','\\.')
["192","168","0","1"]
- trim(去前后空格)
- lpad(左填充)
- rpad(右填充)
- reverse (字符串反转函数)
类型转换函数
- cast(value as type)
select ip,CAST(port as string) as port from instances
其他常用函数
列转行
- collect_list 将列转换成array
- collect_set 将列转换成array并去重
# 一个app_id下可能有多个config_value相关信息,需要把多个config_value放到一个字段
select app_id,collect_set(config_value) from a_r2m_r2m_redis_config group by app_id ;
字符串拼接
- concat_ws(',',array) 将 array中的元素转换成以,分割的字符串
- concat('str','str2')
- collect_set
行转列
# 源表结构
源表(table1)数据{A:string B:array<BIGINT> C:string}
# 行转列
select A,B,C from table_1 LATERAL VIEW explode(B) table1 as B
网友评论