PostgreSQL時(shí)間處理的一些常用方式總結(jié)
1.獲取當(dāng)前時(shí)間
now()函數(shù):
select now();
current_timestamp,同now():
select current_timestamp;
select current_time;
select current_date;
可以去掉now()、掉后面的+8等:
select now()::timestamp(0)without time zone; select current_timestamp::timestamp(0)without time zone;
2.date_part函數(shù)
語(yǔ)法:DATE_PART(field, source), filed可以理解為要截取的類型。
下面是filed支持的類型:
CENTURY,世紀(jì),獲取日期所在的世紀(jì):
select date_part('CENTURY', TIMESTAMP '2022-12-16 12:21:13'); select date_part('CENTURY', now());
MILLENNIUM,千年
select date_part('MILLENNIUM', timestamp '2022-12-16 13:21:15');
YEAR,年份域
select date_part('YEAR', timestamp '2022-12-16 13:21:15');
MONTH,對(duì)于timestamp數(shù)值,它是一年里的月份數(shù)(1-12);對(duì)于interval數(shù)值,它是月的數(shù)目,然后對(duì)12取模(0-11)
select date_part('MONTH', timestamp '2022-12-16 13:21:15');
select date_part('month', interval '2 years 5 months')
DAY,日期里的天,值是1-31:
select date_part('day', TIMESTAMP '2022-12-16 12:21:13'); select date_part('day', now());
HOUR,小時(shí)(0-23)
select date_part('HOUR', TIMESTAMP '2022-12-16 12:21:13');
MINUTE,分鐘域(0-59)
select date_part('MINUTE', TIME '2022-12-16 13:21:15');
SECOND,秒域,包括小數(shù)部分(0-59[1])
select date_part('SECOND', timestamp '2022-12-16 13:21:15');
MICROSECONDS,秒域(包括小數(shù))乘以 1,000,000
select date_part('MICROSECONDS', TIME '2022-12-16 13:21:15');
MILLISECONDS,秒域(包括小數(shù))乘以 1,000
select date_part('MILLISECONDS', timestamp '2022-12-16 13:21:15');
DECADE,年份域除以10:
select date_part('DECADE', TIMESTAMP '2022-12-16 12:21:13');
DOW,星期號(hào)(0-6;星期天是0) (僅用于timestamp)
select date_part('DOW', TIMESTAMP '2022-12-16 12:21:13'); select date_part('DOW', now());
DOY,一年中的第幾天(1 -365/366) (僅用于 timestamp)
select date_part('DOY', TIMESTAMP '2022-12-16 12:21:13');
QUARTER,該天所在的該年的季度(1-4)(僅用于 timestamp)
select date_part('QUARTER', timestamp '2022-12-16 13:21:15');
WEEK,該天在所在的年份里是第幾周。
select date_part('WEEK', timestamp '2022-12-16 13:21:15');
3.extract()函數(shù)
使用語(yǔ)法:extract (field from source),field 支持的類型,和date_part()函數(shù)一樣
select extract ('year' from timestamp '2022-12-16 13:21:15')
4.日期格式化函數(shù)
to_char(timestamp, text),把時(shí)間戳轉(zhuǎn)換成字串
select to_char(now(), 'YYYY-MM-DD HH24:MI:SS')
to_date(text, text) 把字串轉(zhuǎn)換成日期
select to_date('05 Dec 2022', 'DD Mon YYYY')
to_timestamp(text, text) ,把字串轉(zhuǎn)換成時(shí)間戳
select to_timestamp('05 Dec 2022', 'DD Mon YYYY')
5.時(shí)間運(yùn)算
select date '2001-09-28' + integer '7'; select date '2001-09-28' + interval '1 hour'; select date '2001-09-28' + time '03:00'; select interval '1 day' + interval '1 hour'; select timestamp '2001-09-28 01:00' + interval '23 hours'; select time '01:00' + interval '3 hours'; select - interval '23 hours'; select date '2001-10-01' - date '2001-09-28'; select date '2001-10-01' - integer '7'; select date '2001-09-28' - interval '1 hour'; select time '05:00' - time '03:00'; select time '05:00' - interval '2 hours; select timestamp '2001-09-28 23:00' - interval '23 hours'; select interval '1 day' - interval '1 hour'; select timestamp '2001-09-29 03:00' - timestamp '2001-09-27 12:00'; select interval '1 hour' * double precision '3.5'; select interval '1 hour' / double precision '1.5';
6.計(jì)算時(shí)間差
select now() + interval '10 min/year/month/day/hour/sec/ (1 year 1 month 1 day 1 hour 1 min 1 sec)' select now() - interval '10 min/year/month/day/hour/sec/ (1 year 1 month 1 day 1 hour 1 min 1 sec)' select now()::timestamp(0)without time zone-interval '72 hour' select extract(day from now() - '2001-09-27 12:00') from user ;
總結(jié)
到此這篇關(guān)于pgsql時(shí)間處理的一些常用方式總結(jié)的文章就介紹到這了,更多相關(guān)pgsql時(shí)間處理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
如何將excel表格數(shù)據(jù)導(dǎo)入postgresql數(shù)據(jù)庫(kù)
這篇文章主要介紹了如何將excel表格數(shù)據(jù)導(dǎo)入postgresql數(shù)據(jù)庫(kù),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03postgresql查詢每個(gè)月的最后一天日期并對(duì)未查到的日期結(jié)果補(bǔ)0(操作示例)
這篇文章主要介紹了postgresql查詢每個(gè)月的最后一天日期,并對(duì)未查到的日期結(jié)果補(bǔ)0,pgsql需要使用函數(shù)使用date_trunc()函數(shù)找到指定月第一天,然后對(duì)該日期先加一個(gè)月在減一個(gè)月就能得到你傳給的日期的最后一天日期,感興趣的朋友跟隨小編一起看看吧2023-10-10淺談PostgreSQL中的孤兒文件用法(orphaned data files)
這篇文章主要介紹了淺談PostgreSQL中的孤兒文件用法(orphaned data files),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01如何查看PostgreSQL數(shù)據(jù)庫(kù)中所有表
這篇文章主要介紹了如何查看PostgreSQL數(shù)據(jù)庫(kù)中所有表問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03Postgres copy命令導(dǎo)入導(dǎo)出數(shù)據(jù)的操作方法
最近有需要對(duì)數(shù)據(jù)進(jìn)行遷移的需求,由于postgres性能的關(guān)系,單表3000W的數(shù)據(jù)量查詢起來(lái)有一些慢,需要對(duì)大表進(jìn)行切割,拆成若干個(gè)子表,涉及到原有數(shù)據(jù)要遷移到子表的需求,這篇文章主要介紹了Postgres copy命令導(dǎo)入導(dǎo)出數(shù)據(jù)的操作方法,需要的朋友可以參考下2024-08-08Docker環(huán)境下升級(jí)PostgreSQL的步驟方法詳解
這篇文章主要介紹了Docker環(huán)境下升級(jí)PostgreSQL的步驟方法詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01Linux CentOS 7源碼編譯安裝PostgreSQL9.5
這篇文章主要為大家詳細(xì)介紹了Linux CentOS 7源碼編譯安裝PostgreSQL9.5的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11PostgreSQL中Slony-I同步復(fù)制部署教程
這篇文章主要給大家介紹了關(guān)于PostgreSQL中Slony-I同步復(fù)制部署的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用PostgreSQL具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-06-06postgresql無(wú)則插入,有則更新問(wèn)題
這篇文章主要介紹了postgresql無(wú)則插入,有則更新問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04PgSQL條件語(yǔ)句與循環(huán)語(yǔ)句示例代碼詳解
這篇文章主要介紹了PgSQL條件語(yǔ)句與循環(huán)語(yǔ)句,pgSQL中有兩種條件語(yǔ)句分別為if與case語(yǔ)句,每種語(yǔ)句通過(guò)示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07