postgresql常用日期函數(shù)使用整理
簡(jiǎn)介
PostgreSQL 提供了以下日期和時(shí)間運(yùn)算的算術(shù)運(yùn)算符。
獲取當(dāng)前系統(tǒng)時(shí)間
select current_date,current_time,current_timestamp ;
-- 當(dāng)前系統(tǒng)時(shí)間一周后的日期 select current_date + interval '7 day',current_time,current_timestamp ;
計(jì)算時(shí)間間隔
age(timestamp, timestamp)函數(shù)用于計(jì)算兩個(gè)時(shí)間點(diǎn)之間的間隔,age(timestamp)函數(shù)用于
計(jì)算當(dāng)前日期的凌晨 12 點(diǎn)到該時(shí)間點(diǎn)之間的間隔
select age(now(),date '1988-11-29') as ageResult;
獲取時(shí)間中的信息
date_part(text, timestamp)和 extract(field FROM timestamp)函數(shù)用于獲取日期時(shí)間中的某一部分,例如年份、月份、小時(shí)等;date_part(text, interval)和 extract(field FROM interval)函數(shù)
用于獲取時(shí)間間隔中的某一部分
-- 獲取當(dāng)前日期所屬年份 select extract ('year' from now()) as t;
-- 判斷當(dāng)前日期是星期幾 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 "當(dāng)前年度", date_part('month',now()) as "當(dāng)前月份", date_part('day',now()) as "當(dāng)前日子", date_part('dow',now()) as "星期幾" ;
extract 函數(shù)實(shí)際上也是調(diào)用了 date_part 函數(shù),只是參數(shù)方式不同。這兩個(gè)函數(shù)支持獲取的信息包括
- century,世紀(jì);
- day,對(duì)于 timestamp,返回月份中的第幾天;對(duì)于 interval,返回天數(shù);
- decade,年份除以 10;
- dow,星期天(0)到星期六(6);
- doy,一年中的第幾天,(1 - 365/366);
- epoch,對(duì)于 timestamp WITH time zone,返回 1970-01-01 00:00:00 UTC 到該時(shí)間的秒數(shù);
對(duì)于 date 和 timestamp,返回本地時(shí)間的 1970-01-01 00:00:00 到該時(shí)間的秒數(shù);對(duì)于
interval,返回以秒數(shù)表示的該時(shí)間間隔; - hour,小時(shí)(1 - 23);
- isodow,ISO 8601 標(biāo)準(zhǔn)中的星期一(1)到星期天(7)
- isoyear,ISO 8601 標(biāo)準(zhǔn)定義的日期所在的年份。每年從包含 1 月 4 日的星期一開(kāi)始,2017
年 01 月 01 日屬于 2016 年; - microseconds,微秒,包含秒和小數(shù)秒在內(nèi)的數(shù)字乘以 1000000;
- millennium,千年;
- milliseconds,毫秒,包含秒和小數(shù)秒在內(nèi)的數(shù)字乘以 1000;
- minute,分鐘,(0 - 59);
- month,月份;
- quarter,季度,(1 - 4);
- second,秒數(shù),包含小數(shù)秒;
- timezone,UTC 時(shí)區(qū),單位為秒;
- timezone_hour,UTC 時(shí)區(qū)中的小時(shí)部分;
- timezone_minute,UTC 時(shí)區(qū)中的分鐘部分;
- week,ISO 8601 標(biāo)準(zhǔn)中的星期幾,每年從第一個(gè)星期四所在的一周開(kāi)始;
- year,年份。
-- 計(jì)算當(dāng)前日期從1970年到現(xiàn)在的秒數(shù) select extract('epoch' from current_date);
截?cái)嗳掌?時(shí)間
date_trunc(field, source [, time_zone ])函數(shù)用于將 timestamp、timestamp WITH time zone、date、time 或者 interval 數(shù)據(jù)截?cái)嗟街付ǖ木?/p>
date_trunc 函數(shù)支持以下截?cái)嗑龋?/p>
- 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)建日期/時(shí)間
make_date(year int, month int, day int)
函數(shù)用于創(chuàng)建一個(gè)日期: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ù)通過(guò)指定年、月、日等信息創(chuàng)建一個(gè)時(shí)間間隔。make_time(hour int, min int, sec double precision)
函數(shù)通過(guò)指定小時(shí)、分鐘和秒數(shù)創(chuàng)建一個(gè)
時(shí)間。
make_timestamp(year int, month int, day int, hour int, min int, sec double precision)
函數(shù)通過(guò)指定年、月、日、時(shí)、分、秒創(chuàng)建一個(gè)時(shí)間戳make_timestamptz(year int, month int, day int, hour int, min int, sec double precision, [ timezone text ])
函數(shù)通過(guò)指定年、月、日、時(shí)、分、秒創(chuàng)建一個(gè)帶時(shí)區(qū)的時(shí)間戳。如果沒(méi)有指
定時(shí)區(qū),使用當(dāng)前時(shí)區(qū)to_timestamp(double precision)
函數(shù)將 Unix 時(shí)間戳(自從 1970-01-01 00:00:00+00 以來(lái)的秒
數(shù))轉(zhuǎn)換為 PostgreSQL 時(shí)間戳數(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)時(shí)間
PostgreSQL 提供了大量用于獲取系統(tǒng)當(dāng)前日期和時(shí)間的函數(shù),例如 current_date、current_time、
current_timestamp、clock_timestamp()、localtimestamp、now()、statement_timestamp()等;同時(shí)還
支持延遲語(yǔ)句執(zhí)行的 pg_sleep()等函數(shù)
-- 當(dāng)前日期 select current_date as t1, current_time as t2, localtime as t3, current_timestamp as t4, localtimestamp as t5, now() as t6 ;
時(shí)區(qū)轉(zhuǎn)換
AT TIME ZONE 運(yùn)算符用于將 timestamp without time zone、timestamp WITH time zone 以及
time WITH time zone 轉(zhuǎn)換為指定時(shí)區(qū)中的時(shí)間
timezone(zone, timestamp)函數(shù)等價(jià)于 SQL 標(biāo)準(zhǔn)中的 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';
總結(jié)
到此這篇關(guān)于postgresql常用日期函數(shù)使用整理的文章就介紹到這了,更多相關(guān)postgresql常用日期函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
PostgreSQL配置遠(yuǎn)程連接簡(jiǎn)單圖文教程
這篇文章主要給大家介紹了關(guān)于PostgreSQL配置遠(yuǎn)程連接的相關(guān)資料,PostgreSQL是一個(gè)功能非常強(qiáng)大的關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng)(RDBMS),文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12Linux 上 定時(shí)備份postgresql 數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了Linux 上 定時(shí)備份postgresql 數(shù)據(jù)庫(kù)的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02

PostgreSQL基于Citus實(shí)現(xiàn)分布式集群的全過(guò)程

詳解如何在PostgreSQL中使用JSON數(shù)據(jù)類型