hive常用的日期函數


## 當前日期和時間
SELECT current_timestamp();
-- 2018-04-28 11:46:03.136

## 獲取當前日期,當前是 2018-04-28
SELECT current_date;
OR
SELECT current_date();
-- 2018-04-28

## 獲取unix系統下的時間戳
SELECT UNIX_TIMESTAMP();
-- 1524884881

## 當前是 2018-04-28
select substr(current_timestamp, 0, 10);
-- 2018-04-28

## 當前是 2018-04-28
select date_sub(current_date, 1);
-- 2018-04-27

## yyyy-MM-dd HH:MM:ss 截取日期
select to_date("2017-10-22 10:10:10");
-- 2017-10-22

## 兩個日期之間的天數差
select datediff("2017-10-22", "2017-10-12");
-- 10

select datediff("2017-10-22 10:10:10", "2017-10-12 23:10:10");
-- 10

select datediff("2017-10-22 01:10:10", "2017-10-12 23:10:10");
-- 10

## 時間截取
select from_unixtime(cast(substr("1504684212155", 0,10) as int)) dt;
-- 2017-09-06 15:50:12

## 時間戳轉日期
## 語法: to_date(string timestamp) 
select to_date(from_unixtime(UNIX_TIMESTAMP()));
-- 2018-04-28

select FROM_UNIXTIME(UNIX_TIMESTAMP(),'yyyy-MM-dd 10:30:00');
-- 2018-04-28 10:30:00

select concat(date_sub(current_date,1),' 20:30:00');
-- 2018-04-27 20:30:00

-- hive version 1.2.0
select date_format(date_sub(current_date,1),'yyyy-MM-dd 20:30:00');



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM