Hql 時(shí)間轉(zhuǎn)換函數(shù)

函數(shù)總結(jié):
select unix_timestamp('2019-07-01 23:59:00','yyyy-MM-dd HH:mm:ss');

轉(zhuǎn)化時(shí)間戳
select from_unixtime(1561996740);

轉(zhuǎn)回時(shí)間
substr(string A, int start, int len),substring(string A, int start, int len)

時(shí)間轉(zhuǎn)換函數(shù)
substr(wce.create_time,12,2) as hourinfo, 轉(zhuǎn)化小時(shí)
substr(wce.create_time,1,4) as yearinfo, 轉(zhuǎn)換年
quarter(wce.create_time) as quarterinfo, 轉(zhuǎn)化季度
substr(wce.create_time,6,2) as monthinfo, 轉(zhuǎn)化月
substr(wce.create_time,9,2) as dayinfo 轉(zhuǎn)化天

--輸出 2020-01-10
--hive
select to_date(from_unixtime(UNIX_TIMESTAMP('20200110','yyyyMMdd')));

--presto
select (format_datetime(date_parse('20200110','%Y%m%d'),'yyyy-MM-dd') ;

  • 問題2: 時(shí)間的加減

例子: 原時(shí)間為20200110 需先轉(zhuǎn)化為標(biāo)準(zhǔn)日期形式再加減

--hive 
select date_add('2020-01-12',10);
select date_add(to_date(from_unixtime(UNIX_TIMESTAMP('20200110','yyyyMMdd'))),10);

--presto
select date_add('day',-6,cast('2020-07-07' as date)); 
--第三個(gè)參數(shù)不轉(zhuǎn)換為date格式, 會(huì)報(bào)錯(cuò) 第三個(gè)參數(shù)必須為date格式
select 
date_add('day', -6, cast(format_datetime(date_parse('20200110','%Y%m%d'),'yyyy-MM-dd') as date));
  • 問題3: 時(shí)間戳轉(zhuǎn)日期
--hive
select from_unixtime(1578585600);
--加格式
select from_unixtime(1578585600,'yyyyMMdd');

--presto
select from_unixtime(1578585600);
--加格式
select format_datetime(from_unixtime(1578585600),'yyyy-MM-dd');
  • 問題4: 日期轉(zhuǎn)時(shí)間戳
--hive 
select unix_timestamp('20200110' ,'yyyyMMdd'); --10位時(shí)間戳

-- presto 
select to_unixtime(cast('2020-01-10' as date));
select to_unixtime(cast(format_datetime(date_parse('20200110','%Y%m%d'),'yyyy-MM-dd') as date));
  • 問題5: 計(jì)算兩個(gè)日期之間的diff
--hive
select datediff('2017-09-15','2017-09-01');-- 結(jié)果14

--presto 
select date_diff('day',cast('2018-09-05' as date),cast('2018-09-07' as date));

-- 1)需要提供參數(shù)'day',表示要查詢的是天數(shù)間隔;要查詢小時(shí),則提供參數(shù)'hour'
-- 2)并且后面?zhèn)鲄⑾拗茷閐ate類型;
-- 3)最后要注意是后面減去前面 --與hive不同
  • 問題6: 當(dāng)前時(shí)間
--hive
select current_date;
select unix_timestamp(); --獲取當(dāng)前時(shí)間戳
select from_unixtime(unix_timestamp());

-- presto
select now();  --精確到今天的時(shí)分秒
select current_date; --精確到今天的年月日
select current_date - interval '1' day; 精確到昨天的年月日

--hive其它日期查詢
select current_timestamp; --查詢當(dāng)前系統(tǒng)時(shí)間(包括毫秒數(shù));  
select dayofmonth(current_date); -- 查詢當(dāng)月第幾天
select last_day(current_date); --月末
select date_sub(current_date,dayofmonth(current_date)-1); --當(dāng)月第1天: 
select add_months(date_sub(current_date,dayofmonth(current_date)-1),1); --下個(gè)月第1天
  • 1、concat(str1,str2,str3,…) --鏈接兩個(gè)字符串
  • 2、concat_ws(‘分隔符’,str1,str2,…) --用符號(hào)鏈接兩個(gè)字符串
  • 3、group_concat(str1,[order by str3],[separator ‘分隔符’]) --行轉(zhuǎn)列
按id分組,把name連接為一行
select id,group_concat(name)
1|bobannahelen
2|tombabytom
按id分組,把name連接為一行,并按name升序
select id,group_concat(name order by name asc)
1|annabobhelen
2|babytomtom
按id分組,name去重并連接為一行,按name升序,用逗號(hào)分隔
select id,group_concat(distinct name order by name asc separator ‘,’)
1|anna,bob,helen
2|baby,tom

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容