在大多数的sql中获取当前时间都是用now()函数即可,hive获取当前时间的函数与sql 不一样
在impala中执行now()函数时是可以通过的,然而在hive中执行now()函数却报错:
获取当前时间戳
hive有一个获得当前时区的UNIX时间戳:unix_timestamp
语法: unix_timestamp()
返回值: bigint
说明: 获得当前时区的UNIX时间戳
举例:
获取当前日期和时间
我们需要的不是时间戳而是具体的当前时间:from_unixtime
语法: from_unixtime(bigint unixtime[, string format])
后面的格式可以根据需要自己设置
或者用current_timestamp()
和 current_date()
举例:
--1. 获取当前日期和时间(年月日时分秒)
--写法一:
select from_unixtime(unix_timestamp(),"yyyy-MM-dd HH:mm:ss")
2020-04-21 11:02:55
--写法二:
select substr(current_timestamp(),1,19)
2020-04-21 11:02:55
-- 2.获取当前日期
--写法一:
select from_unixtime(unix_timestamp(),"yyyy-MM-dd")
2020-04-21
--写法二:(推荐)
select current_date()或者select current_date
2020-04-21
-- 写法三:
select substr(current_timestamp(),1,10)
2020-04-21
完整时间函数可以参考博文