postgresql常用日期函數(shù)使用整理
簡介
PostgreSQL 提供了以下日期和時間運算的算術運算符。
獲取當前系統(tǒng)時間
select current_date,current_time,current_timestamp ;
-- 當前系統(tǒng)時間一周后的日期 select current_date + interval '7 day',current_time,current_timestamp ;
計算時間間隔
age(timestamp, timestamp)函數(shù)用于計算兩個時間點之間的間隔,age(timestamp)函數(shù)用于
計算當前日期的凌晨 12 點到該時間點之間的間隔
select age(now(),date '1988-11-29') as ageResult;
獲取時間中的信息
date_part(text, timestamp)和 extract(field FROM timestamp)函數(shù)用于獲取日期時間中的某一部分,例如年份、月份、小時等;date_part(text, interval)和 extract(field FROM interval)函數(shù)
用于獲取時間間隔中的某一部分
-- 獲取當前日期所屬年份 select extract ('year' from now()) as t;
-- 判斷當前日期是星期幾 select extract ('dow' from now()) as t;
select date_part('year', timestamp '2020-03-03 20:38:40'), extract(year FROM timestamp '2020-03-03 20:38:40'), date_part('month', interval '1 years 5 months'), extract(month FROM interval '1 years 5 months');
select date_part('year', now()) as "當前年度", date_part('month',now()) as "當前月份", date_part('day',now()) as "當前日子", date_part('dow',now()) as "星期幾" ;
extract 函數(shù)實際上也是調用了 date_part 函數(shù),只是參數(shù)方式不同。這兩個函數(shù)支持獲取的信息包括
- century,世紀;
- day,對于 timestamp,返回月份中的第幾天;對于 interval,返回天數(shù);
- decade,年份除以 10;
- dow,星期天(0)到星期六(6);
- doy,一年中的第幾天,(1 - 365/366);
- epoch,對于 timestamp WITH time zone,返回 1970-01-01 00:00:00 UTC 到該時間的秒數(shù);
對于 date 和 timestamp,返回本地時間的 1970-01-01 00:00:00 到該時間的秒數(shù);對于
interval,返回以秒數(shù)表示的該時間間隔; - hour,小時(1 - 23);
- isodow,ISO 8601 標準中的星期一(1)到星期天(7)
- isoyear,ISO 8601 標準定義的日期所在的年份。每年從包含 1 月 4 日的星期一開始,2017
年 01 月 01 日屬于 2016 年; - microseconds,微秒,包含秒和小數(shù)秒在內的數(shù)字乘以 1000000;
- millennium,千年;
- milliseconds,毫秒,包含秒和小數(shù)秒在內的數(shù)字乘以 1000;
- minute,分鐘,(0 - 59);
- month,月份;
- quarter,季度,(1 - 4);
- second,秒數(shù),包含小數(shù)秒;
- timezone,UTC 時區(qū),單位為秒;
- timezone_hour,UTC 時區(qū)中的小時部分;
- timezone_minute,UTC 時區(qū)中的分鐘部分;
- week,ISO 8601 標準中的星期幾,每年從第一個星期四所在的一周開始;
- year,年份。
-- 計算當前日期從1970年到現(xiàn)在的秒數(shù) select extract('epoch' from current_date);
截斷日期/時間
date_trunc(field, source [, time_zone ])函數(shù)用于將 timestamp、timestamp WITH time zone、date、time 或者 interval 數(shù)據(jù)截斷到指定的精度
date_trunc 函數(shù)支持以下截斷精度:
- microseconds
- milliseconds
- second
- minute
- hour
- day
- week
- month
- quarter
- year
- decade
- century
- millennium
select date_trunc('year', timestamp '2020-03-03 20:38:40'), date_trunc('day', timestamptz '2020-03-03 20:38:40+00', 'asia/shanghai'), date_trunc('hour', interval '2 days 3 hours 40 minutes');
創(chuàng)建日期/時間
make_date(year int, month int, day int)
函數(shù)用于創(chuàng)建一個日期:make_interval(years int DEFAULT 0, months int DEFAULT 0, weeks int DEFAULT 0, days int DEFAULT 0, hours int DEFAULT 0, mins int DEFAULT 0, secs double precision DEFAULT 0.0)
函數(shù)通過指定年、月、日等信息創(chuàng)建一個時間間隔。make_time(hour int, min int, sec double precision)
函數(shù)通過指定小時、分鐘和秒數(shù)創(chuàng)建一個
時間。
make_timestamp(year int, month int, day int, hour int, min int, sec double precision)
函數(shù)通過指定年、月、日、時、分、秒創(chuàng)建一個時間戳make_timestamptz(year int, month int, day int, hour int, min int, sec double precision, [ timezone text ])
函數(shù)通過指定年、月、日、時、分、秒創(chuàng)建一個帶時區(qū)的時間戳。如果沒有指
定時區(qū),使用當前時區(qū)to_timestamp(double precision)
函數(shù)將 Unix 時間戳(自從 1970-01-01 00:00:00+00 以來的秒
數(shù))轉換為 PostgreSQL 時間戳數(shù)據(jù)。
select make_date(2020, 03, 15) as t1, make_interval(days => 1, hours => 5) as t2, make_time(1, 2, 30.5) as t3, make_timestamp(2020, 3, 15, 8, 20, 23.5) as t4, make_timestamptz(2020, 3, 15, 8, 20, 23.5) as t5, to_timestamp(1583152349) as t6 ;
獲取系統(tǒng)時間
PostgreSQL 提供了大量用于獲取系統(tǒng)當前日期和時間的函數(shù),例如 current_date、current_time、
current_timestamp、clock_timestamp()、localtimestamp、now()、statement_timestamp()等;同時還
支持延遲語句執(zhí)行的 pg_sleep()等函數(shù)
-- 當前日期 select current_date as t1, current_time as t2, localtime as t3, current_timestamp as t4, localtimestamp as t5, now() as t6 ;
時區(qū)轉換
AT TIME ZONE 運算符用于將 timestamp without time zone、timestamp WITH time zone 以及
time WITH time zone 轉換為指定時區(qū)中的時間
timezone(zone, timestamp)函數(shù)等價于 SQL 標準中的 timestamp AT TIME ZONE zone。
官網(wǎng)介紹
select timestamp '2020-03-03 20:38:40' at time zone 'asia/shanghai', timestamp with time zone '2020-03-03 20:38:40-05:00' at time zone 'asia/shanghai', time with time zone '20:38:40-05:00' at time zone 'asia/shanghai';
總結
到此這篇關于postgresql常用日期函數(shù)使用整理的文章就介紹到這了,更多相關postgresql常用日期函數(shù)內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Linux 上 定時備份postgresql 數(shù)據(jù)庫的方法
這篇文章主要介紹了Linux 上 定時備份postgresql 數(shù)據(jù)庫的方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02PostgreSQL基于Citus實現(xiàn)分布式集群的全過程
Citus是一個PostgreSQL擴展,它將Postgres轉換為分布式數(shù)據(jù)庫,因此您可以在任何規(guī)模上實現(xiàn)高性能,因客戶的需求,本文詳細闡述了PostgreSQL基于Citus實現(xiàn)的分布式集群的全過程,需要的朋友可以參考下2023-11-11詳解如何在PostgreSQL中使用JSON數(shù)據(jù)類型
JSON(JavaScript Object Notation)是一種輕量級的數(shù)據(jù)交換格式,它采用鍵值對的形式來表示數(shù)據(jù),支持多種數(shù)據(jù)類型,本文給大家介紹了如何在PostgreSQL中使用JSON數(shù)據(jù)類型,需要的朋友可以參考下2024-03-03