Presto日期函数
1、当前时间
select current_date,current_time;
多种类型转时间戳
- 字符串转时间戳 string -> timestamp
select cast('2022-03-22' as timestamp);
select date_parse('2022-04-06 00:03:55','%Y-%m-%d %H:%i:%S')
时间戳格式化 format_datetime(timestamp,‘yyyy-MM-dd HH:mm:ss’)
Bigint转时间戳
Create_time是一个bigint类型的数值
Select from_unixtine(create_time) from db;
时间转bigint
Select to_unixtime(current_date) ;
转年月日/取年月日
select date_format(current_date,'%Y-%m-%d'); //字符串格式化成日期的样子
select date(current_date);
select cast(current_date as date); //这是一个字符串可以转日期的函数
字符串转年月日
select date(cast('2022-02-22 10:28:00' as TIMESTAMP));
select date('2019-02-28');
select date_format(cast('2019-02-28 10:28:00' as TIMESTAMP),'%Y-%m-%d');
select to_date('2019-02-28','yyyy-mm-dd');
Bigint转年月日
date(from_unixtime(1556380800))
select date_format(from_unixtime(1556380800),'%Y-%m-%d');
重点:
日期间隔
date_diff(unit, timestamp1, timestamp2) //bigint
select date_diff('day',cast('2019-04-24' as TIMESTAMP),cast('2019-04-26' as TIMESTAMP)) ;
注意:presto的date_diff是后-前,hive和mysql的date_diff是前-后
求几天前,几天后 interval、date_add
select current_date,(current_date - interval '7' day),date_add('day', 7, current_date);
时间截取函数 date_trunc(unit, x)
--截取月初
select date_trunc('month',current_date);
--截取年初
select date_trunc('year',current_date);
时间提取函数 extract、year、month、day
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);
转int
->timestamp->to_unixtime->int
to_unixtime(timestamp_col)