Hive Sql常用的時間處理類,都在這裏了

hive 常用日期格式轉換

 

把固定日期轉換成時間戳

select unix_timestamp('2020-05-29','yyyy-MM-dd')  --返回結果 1590681600

select unix_timestamp('20200529','yyyyMMdd') --返回結果 1590681600

select unix_timestamp('2020-05-29T13:12:23Z', "yyyy-MM-dd'T'HH:mm:ss'Z'") --返回結果 1590729143

select unix_timestamp('2020-05-29 13:12:23', 'yyyy-MM-dd HH:mm:ss') --返回結果 1590729143

 

把 29/May/2020:11:30:03 +0800 轉成正常格式(yyyy-MM-dd hh:mm:ss)

select from_unixtime(to_unix_timestamp('29/May/2020:11:30:03 +0800', 'dd/MMM/yyy:HH:mm:ss Z'))
--返回結果  2020-05-29 11:30:03

 

時間戳轉換成固定日期

select from_unixtime(1590681600,'yyyy-MM-dd') --返回結果 2020-05-29

select from_unixtime(1590681600,'yyyyMMdd') --返回結果 20200529

select from_unixtime(1590729143,'yyyy-MM-dd HH:mm:ss') --返回結果 2020-05-29 13:12:23

select from_unixtime(1590729143) --返回結果 --2020-05-29 13:12:23 不加默認 yyyy-MM-dd HH:mm:ss 格式

select from_unixtime(unix_timestamp('2020-05-29','yyyy-MM-dd'),'Y年') --返回結果 2020年

select from_unixtime(unix_timestamp('2020-05-29','yyyy-MM-dd'),'yyyy-MM') --返回結果 2020-05

select from_unixtime(unix_timestamp('20200529','yyyyMMdd'),'yyyy-MM-dd')  --返回結果 2020-05-29

select from_unixtime(unix_timestamp('2020-05-29','yyyy-MM-dd'),'EEEE') --返回結果 Friday

select from_unixtime(unix_timestamp('2020-05-29','yyyy-MM-dd'),'w') --返回第幾周(22)

select from_unixtime(unix_timestamp('2020-05-29','yyyy-MM-dd'),'u') -- 返回本週的第幾天

select from_unixtime(unix_timestamp('2020-05-29','yyyy-MM-dd'),'D') -- 本年的第幾天(150)

select from_unixtime(unix_timestamp('2020-05-29','yyyy-MM-dd'),'d') -- 本月的第幾天(29)

 

對日期進行格式化轉換

select date_format('2020-05-29','yyyyMMdd') --返回結果  20200529

 

計算得出星期X,date_code是日期參數,如2020-06-01

select pmod(datediff(date_code, '2012-01-01'), 7) = 0 --當等於0時,這天是'星期日'

select pmod(datediff(date_code, '2012-01-01'), 7) = 1 --當等於1時,這天是'星期一'

select pmod(datediff(date_code, '2012-01-01'), 7) = 2 --當等於2時,這天是'星期二'

select pmod(datediff(date_code, '2012-01-01'), 7) = 3 --當等於3時,這天是'星期三'

select pmod(datediff(date_code, '2012-01-01'), 7) = 4 --當等於4時,這天是'星期四' 

select pmod(datediff(date_code, '2012-01-01'), 7) = 5 --當等於5時,這天是'星期五'

select pmod(datediff(date_code, '2012-01-01'), 7) = 6 --當等於6時,這天是'星期六'

 

返回日期時間字段中的日期部分

select to_date('2020-05-29 13:12:23) -- 返回結果 2020-05-29

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章