【presto】时间函数汇总
生活随笔
收集整理的這篇文章主要介紹了
【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,用于設置一個時間戳的時區:
獲取當前日期: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格式需保持一致。
以下為錯誤示例:
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)
函數支持如下所列的間隔單位:
| second | Seconds |
| minute | Minutes |
| hour | Hours |
| day | Days |
| week | Weeks |
| month | Months |
| quarter | Quarters of a year |
| year | Years |
注意: 與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支持如下單位:
| 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】时间函数汇总的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【C++】初级面试整理
- 下一篇: U9变身U9cloud再度出击,用友这是