Hive中的日期函數


HIve中的日期函數

  今天因為苦惱求某年某月某天是今年的第幾周,查了下hive中的日期函數,才發現有這么多都是俺不懂哩!

大致是如下:

(1)日期時間轉日期函數: to_date語法:   to_date(string timestamp)



返回值: string 說明: 返回日期時間字段中的日期部分。 舉例: hive> select to_date('2022-03-14 05:20:01');
返回結果:
2022-03-14


(2)日期轉年函數: year語法:   year(string date)

 
返回值: int
說明: 返回日期中的年。
舉例: hive> select year('2022-03-14 05:20:01');

返回結果:

2022

(3)日期轉月函數: month語法: month   (string date)

 
返回值: int
說明: 返回日期中的月份。
舉例: hive> select month('2022-03-14 05:20:01');
返回結果:
3

(4)日期轉天函數: day語法: day   (string date)

 
返回值: int
說明: 返回日期中的天。
舉例: hive> select day('2011-12-08 10:03:01');
返回結果:
8
舉例2

hive> select day('2011-12-24');

返回結果:
24

(5)日期轉小時函數: hour語法: hour   (string date)

 
返回值: int
說明: 返回日期中的小時。
舉例: hive> select hour('2011-12-08 10:03:01');

返回結果:
10

(6)日期轉分鍾函數: minute語法: minute   (string date)

 
返回值: int
說明: 返回日期中的分鍾。
舉例: hive> select minute('2011-12-08 10:03:01');

返回結果:
3

(6)日期轉秒函數: second語法: second   (string date)

 
返回值: int
說明: 返回日期中的秒。
舉例: hive> select second('2011-12-08 10:03:01');
返回結果:
1

(7)日期轉周函數: weekofyear語法:   weekofyear (string date)

 
返回值: int
說明: 返回日期在當前的周數。
舉例: hive> select weekofyear('2011-12-08 10:03:01');
返回結果:
49

(8)日期比較函數: datediff語法:   datediff(string enddate, string startdate)

 
返回值: int

說明: 返回結束日期減去開始日期的天數。
舉例: hive> select datediff('2012-12-08','2012-05-09');
返回結果:
213

(9)日期增加函數: date_add語法:   date_add(string startdate, int days)

 
返回值: string
說明: 返回開始日期startdate增加days天后的日期。
舉例: hive> select date_add('2012-12-08',10);

返回結果:
 2012-12-18

(10)日期減少函數: date_sub語法:   date_sub (string startdate, int days)

 
返回值: string
說明: 返回開始日期startdate減少days天后的日期。
舉例: hive> select date_sub('2012-12-08',10);
返回結果:
 2012-11-28

(11)返回這個月的最后一天的日期,忽略時分秒部分(HH:mm:ss) last_day(string date)

返回值:string
 hive> select  last_day('2017-02-17 08:34:23');

返回結果:
2017-02-28

(12)返回當前時間的下一個星期X所對應的日期 如:next_day('2015-01-14', 'TU') = 2015-01-20  以2015-01-14為開始時間,其下一個星期二所對應的日期為2015-01-20

返回值:string
 hive> select next_day('2015-01-14', 'TU') ;

返回結果:
2015-01-20

(13)返回當前時間屬性哪個季度 如quarter('2015-04-08') = 2

 
返回值:int
quarter(date/timestamp/string)

(14)返回當前時間日期


返回值:date hive> select current_date;

返回結果:
2022-03-12

(15)如果給定的時間戳並非UTC,則將其轉化成指定的時區下時間戳

 
返回值:timestamp
from_utc_timestamp(timestamp, string timezone)
hive> select from_utc_timestamp('1970-01-01 08:00:00','PST');

返回結果:
 1970-01-01 00:00:00

(16)如果給定的時間戳指定的時區下時間戳,則將其轉化成UTC下的時間戳 to_utc_timestamp(timestamp, string timezone)

 
返回值:timestamp hive> select to_utc_timestamp('1970-01-01 00:00:00','PST');

返回結果:
 1970-01-01 08:00:00 

(17)返回當前時間戳

 
返回值:timestamp hive> select current_timestamp;
返回結果:
 2017-02-25 00:28:46.724 

(18)返回當前時間下再增加num_months個月的日期  add_months(string start_date, int num_months)

返回值:string
hive> select add_months('2017-02-10', 2);
返回結果:
2017-04-10

    

 

 


免責聲明!

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



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