【presto】时间函数汇总
目录
- 获取当前日期:current_date()
- 获取当前时间:current_time()
- 获取当前日期和时间:current_timestamp()
- 获取当前日期和时间:now()
- 获取当前时区:current_timezone()
- 字符串转时间戳:cast(string, timestamp)
- 字符串转时间戳:date_parse(string, fromat)
- bigint转时间戳
- 时间戳转biging
- 时间戳格式化: format_datetime(timestamp,format)
- 时间戳取年月日
- 字符串转年月日
- bigint转年月日
- 时间间隔:date_diff(unit, timestamp1, timestamp2)
- 几天前后几天后:interval、date_add
- 月初、年初、周一和季度第一天:date_trunc(unit, timestamp)
- 时间提取函数 extract、year、month、day
- 日期是周几:day_of_week()
日期时间运算符
运算符 | 示例 | 结果 |
---|---|---|
+ | date ‘2012-08-08’ + interval ‘2’ day ) | 2012-08-10 |
+ | time ‘01:00’ + interval ‘3’ hour | 04:00:00.000 |
+ | timestamp ‘2012-08-08 01:00’ + interval ‘29’ hour | 2012-08-09 06:00:00.000 |
+ | timestamp ‘2012-10-31 01:00’ + interval ‘1’ month | 2012-11-30 01:00:00.000 |
+ | interval ‘2’ day + interval ‘3’ hour | 2 03:00:00.000 |
+ | interval ‘3’ year + interval ‘5’ month | 3-5 |
- | date ‘2012-08-08’ - interval ‘2’ day | 2012-08-06 |
- | time ‘01:00’ - interval ‘3’ hour | 22:00:00.000 |
- | dtimestamp ‘2012-08-08 01:00’ - interval ‘29’ hour | 2012-08-06 20:00:00.000 |
- | timestamp ‘2012-10-31 01:00’ - interval ‘1’ month | 2012-09-30 01:00:00.000 |
- | interval ‘2’ day - interval ‘3’ hour | 1 21:00:00.000 |
- | interval ‘3’ year - interval ‘5’ month | 2-7 |
时区转换
运算符:AT TIME ZONE
,用于设置一个时间戳的时区:
select timestamp '2022-03-17 01:00 UTC';
2022-03-17 09:00:00select timestamp '2022-03-17 01:00 UTC' AT TIME ZONE 'America/Los_Angeles';
2022-03-17 09:00:00
获取当前日期:current_date()
select current_date();
-- 2022-03-17
获取当前时间:current_time()
select current_time();
-- 17:07:16
获取当前日期和时间:current_timestamp()
select current_timestamp();
-- 2022-03-17 21:17:49.035
获取当前日期和时间:now()
select now();
-- 2022-03-17 21:27:03.604
获取当前时区:current_timezone()
select current_timezone();
-- Asia/Shanghai
字符串转时间戳:cast(string, timestamp)
select cast('2022-03-17' as timestamp);
-- 2022-03-17 00:00:00.0
select cast('2022-03-17 00:00:00' as timestamp);
-- 2022-03-17 00:00:00.0
字符串转时间戳:date_parse(string, fromat)
select date_parse('2022-03-17', '%Y-%m-%d');
-- 2022-03-17 00:00:00.0
select date_parse('2022-03-17 00:00:00', '%Y-%m-%d %H:%i:%S');
-- 2022-03-17 00:00:00.0
注意: 字符串格式和format格式需保持一致。
以下为错误示例:
select date_parse('2022-03-17', '%Y-%m-%d %H:%i:%S');
-- Invalid format: "2022-03-17" is too short
bigint转时间戳
select from_unixtime(1647500800);
-- 2022-03-17 15:06:40.0
时间戳转biging
select to_unixtime(current_date);
-- 1647446400
时间戳格式化: format_datetime(timestamp,format)
select format_datetime(cast('2022-03-17' as timestamp),'yyyy-MM-dd HH:mm:ss');
-- 2022-03-17 00:00:00
select format_datetime(cast('2022-03-17' as timestamp),'yyyy-MM-dd HH');
-- 2022-03-17 00
select date_trunc('second', current_timestamp());
-- 2022-05-03 13:37:12.0
select date_trunc('minute', current_timestamp());
--
时间戳取年月日
select date_format(current_date,'%Y-%m-%d');
select date(current_date);
select cast(current_date as date);
-- 2022-03-17
字符串转年月日
select date(cast('2021-03-17 10:28:00' as TIMESTAMP));
select date('2021-03-17');
select date_format(cast('2021-03-17 10:28:00' as TIMESTAMP),'%Y-%m-%d');
select to_date('2021-03-17','yyyy-mm-dd');-- 2021-03-17
注意: 格式不同时date、to_date无法使用
select date('2021-03-17 10:28:00');
-- Value cannot be cast to date: 2021-03-17 10:28:00
select to_date('2021-03-17 10:28:00','yyyy-mm-dd');
-- Invalid format: "2021-03-17 10:28:00" is malformed at " 10:28:00"
bigint转年月日
select date(from_unixtime(1647500800));
-- 2022-03-17
select date_format(from_unixtime(1647500800),'%Y-%m-%d');
-- 2022-03-17
时间间隔:date_diff(unit, timestamp1, timestamp2)
函数支持如下所列的间隔单位:
Unit | Description |
---|---|
second | Seconds |
minute | Minutes |
hour | Hours |
day | Days |
week | Weeks |
month | Months |
quarter | Quarters of a year |
year | Years |
select date_diff('day',cast('2022-03-17' as TIMESTAMP),cast('2022-03-26' as TIMESTAMP));
-- 9
select date_diff('month',cast('2022-02-17' as TIMESTAMP),cast('2022-03-26' as TIMESTAMP));
-- 1
select date_diff('year',cast('2021-02-17' as TIMESTAMP),cast('2022-03-26' as TIMESTAMP));
-- 1
注意: 与hive差异!!!
presto中 date_diff('day',date1,date2)【后-前】
hive,mysql中 datediff(date1,date2) 【前-后】
几天前后几天后:interval、date_add
select current_date, (current_date - interval '7' month), date_add('day', -7, current_date);
-- 2022-03-17 | 2021-08-17 | 2022-03-10
select current_date, (current_date + interval '7' month), date_add('day', 7, current_date);
-- 2022-03-17 | 2022-10-17 | 2022-03-24
月初、年初、周一和季度第一天:date_trunc(unit, timestamp)
函数 date_trunc支持如下单位:
单位 | Example Truncated Value |
---|---|
second | 2001-08-22 03:04:05.000 |
minute | 2001-08-22 03:04:00.000 |
hour | 2001-08-22 03:00:00.000 |
day | 2001-08-22 00:00:00.000 |
week | 2001-08-20 00:00:00.000 |
month | 2001-08-01 00:00:00.000 |
quarter | 2001-07-01 00:00:00.000 |
year | 2001-01-01 00:00:00.000 |
上面的例子使用时间戳: 2001-08-22 03:04:05.321 作为输入。
select date_trunc('second', current_timestamp());
-- 2022-05-03 13:39:38.0
select date_trunc('minute', current_timestamp());
-- 2022-05-03 13:40:00.0
select date_trunc('hour', current_timestamp());
-- 2022-05-03 13:00:00.0
select date_trunc('day', current_timestamp());
-- 2022-05-03 00:00:00.0
select date_trunc('week', cast('2022-03-17' as date));
-- 2022-03-14
select date_trunc('month', cast('2022-03-17' as date));
-- 2022-03-01
select date_trunc('quarter', cast('2022-03-17' as date));
-- 2022-01-01
select date_trunc('year', cast('2022-03-17' as date));
-- 2022-01-01
时间提取函数 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);
-- 2022 | 2022 | 3 | 3 | 17 | 17
日期是周几:day_of_week()
select day_of_week(current_date)
-- 4
【presto】时间函数汇总相关推荐
- 处理时间_7_60个Mysql日期时间函数汇总
Mysql日期时间函数使用大全 需求描述 需求:列出mysql常见的日期.时间函数的使用方法以及注意事项. 解决方法:通过参考官方手册并验证来完成该需求. 注:1 当前以mysql来演示. 2 详细函 ...
- presto 时间函数
时间函数 0.当前日期/当前时间 presto:adm> select current_date,current_time,current_timestamp[=now()]-> ;_co ...
- Mysql 中的日期时间函数汇总
日期和时间函数 MySQL中内置了大量的日期和时间函数,能够灵活.方便地处理日期和时间数据,本节就简单介绍一下MySQL中内置的日期和时间函数. 1 CURDATE()函数 CURDATE()函数用于 ...
- db2取数据库日期时间_DB2日期和时间函数汇总
1.CURRENT DATE获取当前日期:CURRENT TIME获取当前时间:CURRENT TIMESTAMP获取当前时间戳(含年.月.日.时.分.秒):YEAR()获取年:MONTH()获取月: ...
- Windows下获取当前时间函数汇总
(1)使用C标准库(精确到秒级): #include <time.h> #include <stdio.h> int main( void ) { time_t t = tim ...
- Python时间函数汇总
一.time模块 1.time.time() import time# 返回当前时间的时间戳,语法:time.time() timestamp=time.time() print(timestamp) ...
- DB2日期和时间函数汇总
上一篇提到过在DB2中,可以通过SYSIBM.SYSDUMMY1.SYSIBM.DUAL获取寄存器中的值,也可以通过VALUES关键字获取寄存器中的值.则在这篇中,我们直接用VALUES关键字来看看这 ...
- Jmeter 时间函数工具汇总
在使用Jmeter过程中,常使用的函数汇总 __time : 获取时间戳.格式化时间 ${__time(yyyy-MM-dd HH:mm:ss:SSS,time)} :格式化生成时间格式 2018- ...
- SQL分析在2020年度第一季度的购买人数,销售金额,客单价,客单件人均购买频次(时间函数、分组汇总、常用指标计算)
[面试题]某公司数据库里有3张表,销售订单表.产品明细表.销售网点表 "销售订单表"记录了销售情况,每一张数据表示哪位顾客.在哪一天.哪个网点购买了什么产品,购买的数量是多少,以及 ...
最新文章
- 洛谷P2429 制杖题 [2017年6月计划 数论10]
- Leangoo英文版来了~
- localparam和parameter的区别
- html的区域大小,JavaScript位置与大小(1)之正确理解和运用与尺寸大小相关的DOM属性...
- 史上最全Java多线程面试题及答案
- 不忘每份支持,网易云信感谢一路有你
- 【面试题视频讲解】TreeSet使用示例
- el-input输入金额,保留两位小数
- 文件手动删除后 同步到git
- 学习笔记之grub应用
- linux下如何部署php,linux如何部署php
- 驰为HI8刷Win10系统出现红屏错误如何解决
- 单片机/开发板连接配置的三种方式
- 2017第15届上海国际礼品、赠品及家居用品展览会会刊(参展商名录)
- Keil C51 V6.12
- 贵金属交易最佳时间,2023伦敦金交易平台最新排行榜
- mysql 大量写入 优化_MYSQL大批量写入之性能优化
- Scrolling and zooming chart with ChartScroller
- python函数由什么组成_python的函数
- JSP——JSTL定制标签 - 递归标签显示属性结构
热门文章
- 索尼相机A卡格式化MP4视频的数据恢复案例
- Jenkins+cocoapods+pgy+多版本 持续集成
- 网络游戏防沉迷系统7月16日起正式执行
- VMware中的虚拟机如何配置上网(Linux系统为例)
- 删除visio背景中的多余空白部分
- android测试版微信7.0下载地址,微信7.0.17内测版怎么下载?微信安卓7.0.17内测版下载地址及更新内容...
- 安卓3d游戏开发视频!面试一路绿灯Offer拿到手软,详细的Android学习指南
- 2021年广东省房地产发展概况分析:广州市商品住宅成交面积1222万㎡[图]
- 设计模式(四)——原型模式详解
- 在线教育项目04_讲师管理前端开发