hive中兩種日期格式的轉換

2021-08-27 04:50:34 字數 1563 閱讀 3641

在解析埋點資料時會遇到兩種不同的日期格式:yyyymmdd和yyyy-mm-dd,此型別之間的轉換主要有兩種思路:

第一種方法:from_unixtime+unix_timestamp

--20180905轉成2018-09-05

select from_unixtime(unix_timestamp('20180905','yyyymmdd'),'yyyy-mm-dd')

from dw.ceshi_data

--結果如下:

2018-09-05

--2018-09-05轉成20180905

select from_unixtime(unix_timestamp('2018-09-05','yyyy-mm-dd'),'yyyymmdd')

from dw.ceshi_data

--結果如下:

20180905

第二種方法:substr + concat

--20180905轉成2018-09-05 

select concat(substr('20180905',1,4),'-',substr('20180905',5,2),'-',substr('20180905',7,2)) from dw.ceshi_data

結果如下:

2018-09-05

--2018-09-05轉成20180905

select concat(substr('2018-09-05',1,4),substr('2018-09-05',6,2),substr('2018-09-05',9,2)) from dw.ceshi_data

結果如下:

20180905

下面主要講解from_unixtime和unix_timestamp兩種函式:

from_unixtime:時間戳轉日期函式

用法:from_unixtime(bigint unixtime[, stringformat])

返回值: string

說明: 轉化時間戳到當前時區的時間格式

select from_unixtime(1423306743,'yyyymmdd') 

from dw.ceshi_data;

結果如下:

20150207

unix_timestamp:日期轉時間戳函式

用法:unix_timestamp(string date)

返回值:   bigint

說明: 轉換格式為「yyyy-mm-dd hh:mm:ss「的日期到unix時間戳。如果轉化失敗,則返回0。

select unix_timestamp('2018-09-05 12:01:03') 

from dw.ceshi_data;

結果如下:

1536120063

--獲取當前日期的時間戳:

select unix_timestamp()

from dw.ceshi_data;

結果如下:

1536126324

hive中兩種日期格式的轉換

在解析埋點資料時會遇到兩種不同的日期格式 yyyymmdd和yyyy mm dd,此型別之間的轉換主要有兩種思路 第一種方法 from unixtime unix timestamp 20180905轉成2018 09 05 select from unixtime unix timestamp 2...

Hive中的兩種行列轉換

2.字串合併與拆分形式的行列轉換 總結資料如下 name item score 張三 數學 58 張三 英語 83 張三 語文 89 李四 數學 67 李四 英語 35 李四 語文 92 王五 數學 75 王五 英語 88 王五 語文 70 要求將每個人各科成績打在一行上,分成三列顯示,即,目標資料...

資料庫 HIVE SQL中兩種日期的轉換

工作中,經常遇到各種格式的日期形式,而且又需要進行關聯操作,這個時候怎麼辦呢?之前每次都是用到的時候各種查資料。這次就總結一下,相關函式,後期不定時持續更新 exp1 2018 11 5 和 2018 11 05 兩種型別的進行日期關聯?在日常使用時,可能都用來取時間戳了,比如 select uni...