Presto日期函数

1、当前时间

select current_date,current_time;

多种类型转时间戳

  1. 字符串转时间戳  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)
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值