hive常用的时间日期函数

1.unix_timestamp()
返回当前时区的unix时间戳
返回类型:bigint
hive (tmp)> select unix_timestamp() from hive_sum limit 1;
1465875016

2.from_unixtime(bigint unixtime[,string format])
时间戳转日期函数
返回类型:string
hive (tmp)> select from_unixtime(unix_timestamp(),’yyyyMMdd’) from hive_sum limit 1;
20160614

3.unix_timestamp(string date)
返回指定日期格式的的时间戳
返回类型:bigint
注意:如果后面只有date参数,date的形式必须为’yyyy-MM-dd HH:mm:ss’的形式。
hive (tmp)> select unix_timestamp(‘2016-06-01’) from hive_sum limit 1;
NULL
hive (tmp)> select unix_timestamp(‘2016-06-01 00:00:00’) from hive_sum limit 1;
1464710400

4.unix_timestamp(string date,string pattern)
返回指定日期格式的时间戳
返回类型:bigint
hive (tmp)> select unix_timestamp(‘2016-06-01’,’yyyyMMdd’) from hive_sum limit 1;
1449331200

5.to_date(string date)
返回时间字段中的日期部分
返回类型:string
hive (tmp)> select to_date(‘2016-06-01 00:00:00’) from hive_sum limit 1;
2016-06-01

6.year(string date)
返回时间字段中的年
返回类型:int
hive (tmp)> select year(‘2016-06-01 00:00:00’) from hive_sum limit 1;
2016

7.month(string date)
返回时间字段中的月
返回类型:int
hive (tmp)> select month(‘2016-06-01’) from hive_sum limit 1;
6

8.day(string date)
返回时间字段中的天
返回类型:int
hive (tmp)> select day(‘2016-06-01’) from hive_sum limit 1;
1

9.weekofyear(string date)
返回时间字段是本年的第多少周
返回类型:int
hive (tmp)> select weekofyear(‘2016-06-01’) from hive_sum limit 1;
22

10.datediff(string enddate,string begindate)
返回enddate与begindate之间的时间差的天数
返回类型:int
hive (tmp)> select datediff(‘2016-06-01’,’2016-05-01’) from hive_sum limit 1;
31

11.date_add(string date,int days)
返回date增加days天后的日期
返回类型:string
hive (tmp)> select date_add(‘2016-06-01’,15) from hive_sum limit 1;
2016-06-16

12.date_sub(string date,int days)
返回date减少days天后的日期
返回类型:string
hive (tmp)> select date_sub(‘2016-06-01’,15) from hive_sum limit 1;
2016-05-17

13 date_format 的语法: date_format(date/timestamp/string ts, string fmt)

需要注意的是 这里的date 只能支持中杠格式"2020-01-15" 不支持"20200115"或者"2020/01/15"

14、转换函数

select cast(substring('2016-06-05 00:00:00.0',1,10) as int);

结果---》20160605

15、替换函数

select  cast(substring(regexp_replace('2016-06-05 00:00:00.0', '-', ''),1,8) as int);

结果--》20160605

参考:

https://blog.csdn.net/high2011/article/details/52311824

发布了42 篇原创文章 · 获赞 6 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/zkyxgs518/article/details/103973747
今日推荐