日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【presto】时间函数汇总

發布時間:2024/1/18 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【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’ hour04:00:00.000
+timestamp ‘2012-08-08 01:00’ + interval ‘29’ hour2012-08-09 06:00:00.000
+timestamp ‘2012-10-31 01:00’ + interval ‘1’ month2012-11-30 01:00:00.000
+interval ‘2’ day + interval ‘3’ hour2 03:00:00.000
+interval ‘3’ year + interval ‘5’ month3-5
-date ‘2012-08-08’ - interval ‘2’ day2012-08-06
-time ‘01:00’ - interval ‘3’ hour22:00:00.000
-dtimestamp ‘2012-08-08 01:00’ - interval ‘29’ hour2012-08-06 20:00:00.000
-timestamp ‘2012-10-31 01:00’ - interval ‘1’ month2012-09-30 01:00:00.000
-interval ‘2’ day - interval ‘3’ hour1 21:00:00.000
-interval ‘3’ year - interval ‘5’ month2-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)

函數支持如下所列的間隔單位:

UnitDescription
secondSeconds
minuteMinutes
hourHours
dayDays
weekWeeks
monthMonths
quarterQuarters of a year
yearYears
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
second2001-08-22 03:04:05.000
minute2001-08-22 03:04:00.000
hour2001-08-22 03:00:00.000
day2001-08-22 00:00:00.000
week2001-08-20 00:00:00.000
month2001-08-01 00:00:00.000
quarter2001-07-01 00:00:00.000
year2001-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】时间函数汇总的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。