hive日期函數 整理__函數

來源:互聯網
上載者:User



1、日期函數

1)時間戳記:unix_timestamp/from_unixtime

select unix_timestamp();  --查詢目前時間的時間戳記,返回 1486524284
select unix_timestamp('2017-02-08 11:15:50');  --查詢指定時間的時間戳記,返回 1486523750 (若轉換失敗返回0)
select unix_timestamp('20170208 11:00:00','yyyyMMdd HH:mm:ss');  --轉換指定格式時間的時間戳記
select from_unixtime(1486523750);  --查詢指定時間戳記的時間,預設格式yyyy-MM-dd HH:mm:ss ,返回 2017-02-08 11:15:50
select from_unixtime(1486523750,'yyyyMMdd');  --查詢指定時間戳記的時間,轉換成指定格式,返回 20170208

2)日期提取年月日時分秒+周:year/month/day/hour/minute/second/weekofyear

select year('2017-02-08 11:15:50'),month('2017-02-08 11:15:50'),day('2017-02-08 11:15:50'),hour('2017-02-08 11:15:50'),minute('2017-02-08 11:15:50'),second('2017-02-08 11:15:50'),weekofyear('2017-02-08 11:15:50');  --查詢指定日期的年月日時分秒和周,返回 2017、2、8、11、15、50、6

3)日期時間轉日期:to_date

select to_date('2017-02-08 11:15:50'); --查詢指定日期時間中的日期。返回 2017-02-08

4)日期比較

--日期比較,結束時間距離開始時間的天數。datediff(string enddate, string startdate)
select datediff('2017-02-02','2017-01-31');--返回 2
--日期增加和減少。date_sub (string startdate, int days)
select date_add('2017-01-31',2); --返回 2017-02-02
select date_sub('2017-02-02',2);  --返回 2017-01-31

5)日期位移

--自訂日期操作函數(返回帶橫線的日期):get_date
select get_date();--返回當前日期,返回  2017-02-09
select get_date(-2);--返回當前日期往前位移2天的日期 ,返回  2017-02-07
--自訂日期操作函數(返回不帶橫線的日期):get_dt_date
select get_dt_date();--擷取當前日期,返回 20170209
select get_dt_date(get_date(-2));--擷取當前日期位移,轉為不帶橫杆的格式
select get_dt_date('2017-02-02',-2);--20170131

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.