Hive SQL 查询时间

source:Hive 日期时间操作_TriumPhSK的博客-CSDN博客_hive 时间

(1)获取日期

hive> select from_unixtime(unix_timestamp(),"yyyy-MM-dd");
+-------------+
|     _c0     |
+-------------+
| 2022-06-24  |
+-------------+

hive> select current_date;
+-------------+
|     _c0     |
+-------------+
| 2022-06-24  |
+-------------+

(2)获取带时间的日期

-- 获取当前时间,从第一位开始获取,截取19个字符;
hive> select substr(current_timestamp(),1,19);
+----------------------+
|         _c0          |
+----------------------+
| 2022-06-24 14:45:44  |
+----------------------+

-- 获取带有时间戳的时间;
hive> select current_timestamp;
+--------------------------+
|           _c0            |
+--------------------------+
| 2022-06-24 14:48:01.597  |
+--------------------------+

(3)获取某一日期前后几天的日期

hive> select add_months(current_timestamp, -6);
+-------------+
|     _c0     |
+-------------+
| 2021-12-24  |
+-------------+
 
hive> select add_months(current_date,-6);
+-------------+
|     _c0     |
+-------------+
| 2021-12-24  |
+-------------+
 
hive> select add_months('2020-12-01',-6);
+-------------+
|     _c0     |
+-------------+
| 2020-06-01  |
+-------------+


hive> select date_sub(current_date,6);
+-------------+
|     _c0     |
+-------------+
| 2022-06-18  |
+-------------+
 
hive> select date_add(current_date,6);
+-------------+
|     _c0     |
+-------------+
| 2022-06-30  |
+-------------+
 

(4) 获取当前年月日周

select year(current_timestamp);   ----返回日期中的年
select month(current_timestamp);  ----返回日期中的月
select day(current_timestamp);    ----返回日期中的日
select hour(current_timestamp);   ----返回日期中的时
select minute(current_timestamp); ----返回日期中的分
select second(current_timestamp); ----返回日期中的秒
select weekofyear(current_timestamp); ----返回日期在当前的周数

(5)时间戳转日期格式

hive> select from_unixtime(1606989058,'yyyy-MM-dd HH:mm:ss');
+----------------------+
|         _c0          |
+----------------------+
| 2020-12-03 17:50:58  |
+----------------------+

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值