时间函数
0、当前日期/当前时间
presto:adm> select current_date,current_time,current_timestamp【=now()】
-> ;
_col0 | _col1 | _col2
------------+------------------+-----------------------------
2019-04-28 | 13:04:22.232 PRC | 2019-04-28 13:04:22.232 PRC
1、转时间戳
1)字符串转时间戳 (推荐)
即:‘2019-04-26’ 转换成 2019-04-26 00:00:00.000
select cast('2019-04-26' as timestamp) 2019-04-26 00:00:00.000
select cast('2019-04-26 01:22:23' as timestamp) 2019-04-26 01:22:23.000
2)按照format指定的格式,将字符串string解析成timestamp。
select date_parse('2019-04-06','%Y-%m-%d') 2019-04-06 00:00:00.000
select date_parse('2019-04-06 00:03:55','%Y-%m-%d %H:%i:%S') 2019-04-06 00:03:55.000
注:字符串格式和format格式需保持一致,以下为错误示例:
select date_parse('2019-04-06','%Y-%m-%d %H:%i:%S')
Invalid format: "2019-04-06" is too short
select date_parse('2019-04-06 00:03:55','%Y-%m-%d')
Invalid format: "2019-04-06 00:03:55" is malformed at " 00:03:55"
select date_parse('2019-04-06 00:03:55','%Y%m%d %H:%i:%S')
Invalid format: "2019-04-06 00:03:55" is malformed at "-04-06 00:03:55"
注:时间戳格式化 format_datetime(timestamp,‘yyyy-MM-dd HH:mm:ss’)
3)bigint 转时间戳
即:int型 转换成 2017-05-10 06:18:50.000
from_unixtime(create_time)
补充:时间转bigint:
select to_unixtime(current_date); 1556380800
2、转年月日/取年月日
推荐思路:先转时间戳,再格式化为年月日再date()为年月日。
1)时间戳取年月日
即:2017-09-18 13:40:31 转换成 2017-09-18
select date_format(current_date,'%Y-%m-%d')
select date(current_date)
select cast(current_date as date)
-- 2019-04-28
2)字符串转年月日
select date(cast('2019-04-28 10:28:00' as TIMESTAMP))
select date('2019-04-28')
select date_format(cast('2019-04-28 10:28:00' as TIMESTAMP),'%Y-%m-%d')
select to_date('2019-04-28','yyyy-mm-dd');
-- 2019-04-28
注:格式不同时date、to_date无法使用
select date('2019-04-28 10:28:00')
-- failed: Value cannot be cast to date: 2019-04-28 10:28:00
select to_date('2019-04-28 10:28:00','yyyy-mm-dd');
-- Invalid format: "2019-04-28 10:28:00" is malformed at " 10:28:00"
3)bigint 转年月日
date(from_unixtime(1556380800))
select date_format(from_unixtime(1556380800),'%Y-%m-%d')
-- 2019-04-28
3、日期变换:间隔、加减、截取、提取
1)求时间间隔 date_diff
date_diff(unit, timestamp1, timestamp2) → bigint
eg:select date_diff('day',cast('2019-04-24' as TIMESTAMP),cast('2019-04-26' as TIMESTAMP))
--2
注:与hive差异!!!
presto中 date_diff('day',date1,date2)【后-前】
hive,mysql中 datediff(date1,date2) 【前-后】
2)求几天前,几天后 interval、date_add
select current_date,(current_date - interval '7' day),date_add('day', -7, current_date)
2019-04-28 | 2019-04-21 | 2019-04-21
select current_date,(current_date + interval '7' day),date_add('day', 7, current_date)
2019-04-28 | 2019-05-05 | 2019-05-05
3)时间截取函数 date_trunc(unit, x)
截取月初
select date_trunc('month',current_date)
2019-04-01
截取年初
select date_trunc('year',current_date)
2019-01-01
4)时间提取函数 extract、year、month、day
extract(field FROM x) → bigint【注:field不带引号!】
year(x),month(x),day(x)
eg:
select
extract(year from current_date),
year(current_date),
extract(month from current_date),
month(current_date),
extract(day from current_date),
day(current_date);
-------+-------+-------+-------+-------+-------
2019 | 2019 | 4 | 4 | 28 | 28
4、转int
思路:先转timestamp,再to_unixtime转int
to_unixtime(timestamp_col)