欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PostgreSQL設(shè)置時(shí)區(qū)、時(shí)間/日期函數(shù)匯總大全

 更新時(shí)間:2023年09月28日 16:38:36   作者:一碗情深  
PostgreSQL是一款簡(jiǎn)介而又性能強(qiáng)大的數(shù)據(jù)庫(kù)應(yīng)用程序,其在日期時(shí)間數(shù)據(jù)方面所支持的功能也都非常給力,這篇文章主要給大家介紹了關(guān)于PostgreSQL設(shè)置時(shí)區(qū)、時(shí)間/日期函數(shù)的相關(guān)資料,需要的朋友可以參考下

前言

本文基于 PostgreSQL 12.6 版本,不同版本的函數(shù)可能存在差異。

查看版本 psql --version。

查看時(shí)區(qū)

show timezone; --UTC
select now(); -- 2023-07-24 09:22:48.589640 +00:00

視圖 pg_timezone_names 保存了所有可供選擇的時(shí)區(qū)

select * from pg_timezone_names;

查詢 PRC 時(shí)區(qū)

select * from pg_timezone_names where name = 'PRC';

PRC是指中華人民共和國(guó) PRC(People’s Republic of China)。

修改時(shí)區(qū)

修改時(shí)區(qū),設(shè)置成東八區(qū) 北京時(shí)間 UTC+8,默認(rèn)為session級(jí)配置

set time zone 'PRC';
select now(); -- 2023-07-24 17:21:05.086183 +08:00

修改時(shí)區(qū),用戶級(jí)配置

alter role rolname set timezone='UTC'; -- 修改指定角色時(shí)區(qū)(rolname為角色名)
alter role all set timezone='UTC';     -- 修改所有角色時(shí)區(qū)

修改時(shí)區(qū),數(shù)據(jù)庫(kù)級(jí)配置

alter database dbname set timezone='UTC'; -- dbname為數(shù)據(jù)庫(kù)名稱

時(shí)間/日期操作符和函數(shù)

時(shí)間/日期操作符

操作符例子返回類型結(jié)果
+select date ‘2023-07-24’ + integer ‘7’;date2023-07-31
+select date ‘2023-07-24’ + interval ‘1 hour’;timestamp2023-07-24 01:00:00.000000
+select date ‘2023-07-24’ + time ‘15:16’;timestamp2023-07-24 15:16:00.000000
+select interval ‘1 day’ + interval ‘1 hour’;interval0 years 0 mons 1 days 1 hours 0 mins 0.0 secs
+select timestamp ‘2023-07-24 15:16’ + interval ‘23 hours’;timestamp2023-07-25 14:16:00.000000
+select time ‘01:00’ + interval ‘3 hours’;time04:00:00
-select - interval ‘23 hours’;interval0 years 0 mons 0 days -23 hours 0 mins 0.0 secs
-select date ‘2023-07-24’ - date ‘2023-07-22’;integer2
-select date ‘2023-07-24’ - integer ‘7’;date2023-07-17
-select date ‘2023-07-24’ - interval ‘1 hour’;timestamp2023-07-23 23:00:00.000000
-select time ‘05:00’ - time ‘03:00’;interval0 years 0 mons 0 days 2 hours 0 mins 0.0 secs
-select time ‘05:00’ - interval ‘2 hours’;time03:00:00
-select timestamp ‘2023-07-24 23:00’ - interval ‘23 hours’;timestamp2023-07-24 00:00:00.000000
-select interval ‘1 day’ - interval ‘1 hour’;interval0 years 0 mons 1 days -1 hours 0 mins 0.0 secs
-select timestamp ‘2023-07-24 03:00’ - timestamp ‘2023-07-24 12:00’;interval0 years 0 mons 0 days -9 hours 0 mins 0.0 secs
*select interval ‘1 hour’ * double precision ‘3.5’;interval0 years 0 mons 0 days 3 hours 30 mins 0.0 secs
/select interval ‘1 hour’ / double precision ‘1.5’;interval0 years 0 mons 0 days 0 hours 40 mins 0.0 secs

日期/時(shí)間函數(shù):

函數(shù)描述例子返回類型結(jié)果
age(timestamp, timestamp)第1個(gè)timestamp 減去 第2個(gè)timestampselect age(‘2023-07-24’, ‘1997-10-26’);interval25 years 8 mons 29 days 0 hours 0 mins 0.0 secs
age(timestamp)從current_date 減去 timestamp的值select age(timestamp ‘1997-10-26’);interval25 years 8 mons 29 days 0 hours 0 mins 0.0 secs
current_date今天的日期select current_date;date2023-07-24
current_time現(xiàn)在的時(shí)間select current_time;time07:53:43.911756 +00:00
current_timestamp日期和時(shí)間select current_timestamp;timestamp2023-07-24 07:54:19.495372 +00:00
date_part(text, timestamp)獲取子域(等效于extract)select date_part(‘hour’, timestamp ‘2023-07-24 15:56:34’);double15
date_part(text, interval)獲取子域(等效于extract)select date_part(‘month’, interval ‘2 years 3 months’);double3
date_trunc(text, timestamp)截?cái)喑芍付ǖ木?/td>select date_trunc(‘hour’, timestamp ‘2023-07-24 15:56:34’);timestamp2023-07-24 15:00:00.000000
extract(field from timestamp)獲取子域select extract(hour from timestamp ‘2023-07-24 15:56:34’);double15
extract(field from interval)獲取子域select extract(month from interval ‘2 years 3 months’);double3
localtime當(dāng)前時(shí)間select localtime;time08:00:08
localtimestamp當(dāng)前日期和時(shí)間select localtimestamp;timestamp2023-07-24 08:05:03.650472
now()當(dāng)前的日期和時(shí)間(等效于current_timestamp)select now();timestamp2023-07-24 08:09:30.828408 +00:00
timeofday()當(dāng)前日期和時(shí)間select timeofday();textMon Jul 24 08:09:51.870484 2023 UTC

extract,date_part函數(shù)支持的field

extract,date_part 這兩個(gè)函數(shù)可以從日期時(shí)間值中提取指定的部分,例如年份、月份、小時(shí)等。extract 是一個(gè) PostgreSQL 特有的函數(shù),而 date_part 在標(biāo)準(zhǔn) SQL 中也有定義,但兩者的功能類似。

描述例子結(jié)果
century世紀(jì)select extract(century from timestamp ‘2023-07-24 15:56:34’);21
day(月份)里的日期域(1-31)select extract(day from timestamp ‘2023-07-24 15:56:34’);24
decade年份域除以10select extract(decade from timestamp ‘2023-07-24 15:56:34’);202
dow每周的星期號(hào)(0-6;星期天是0) (僅用于timestamp)select extract(dow from timestamp ‘2023-07-24 15:56:34’);1
doy一年的第幾天(1 -365/366) (僅用于 timestamp)select extract(doy from timestamp ‘2023-07-24 15:56:34’);205
epochUnix時(shí)間戳select extract(epoch from timestamp ‘2023-07-24 15:56:34’);1690214194
hour小時(shí)域(0-23)select extract(hour from timestamp ‘2023-07-24 15:56:34’);15
isodowISO 周幾(1-7,其中1代表星期一)select extract(isodow from timestamp ‘2023-07-24 15:56:34’);1
isoyearISO 年份select extract(isoyear from timestamp ‘2023-07-24 15:56:34’);2023
millennium千年((年份/1000)+1)select extract(millennium from timestamp ‘2023-07-24 15:56:34’);3
microseconds微秒select extract(microseconds from TIME ‘15:56:34.5’);34500000
millisecond毫秒select extract(millisecon from TIME ‘15:56:34.5’);34500
minute分鐘(0-59)select extract(minute from timestamp ‘2023-07-24 15:56:34’);56
month月份,對(duì)于timestamp數(shù)值,它是一年里的月份數(shù)(1-12);對(duì)于interval數(shù)值,它是月的數(shù)目,然后對(duì)12取模(0-11)select extract(month from timestamp ‘2023-07-24 15:56:34’);7
quarter季度,該天所在的該年的季度(1-4)(僅用于 timestamp)select extract(quarter from timestamp ‘2023-07-24 15:56:34’);3
second秒域,包括小數(shù)部分(0-59[1])select extract(second from timestamp ‘2023-07-24 15:56:34’);34
week該天在所在的年份里是第幾周。select extract(week from timestamp ‘2023-07-24 15:56:34’);30
year年份域select extract(year from timestamp ‘2023-07-24 15:56:34’);2023

數(shù)據(jù)類型格式化函數(shù)

PostgreSQL格式化函數(shù)提供一套有效的工具用于把各種數(shù)據(jù)類型(日期/時(shí)間、integer、floating point和numeric)轉(zhuǎn)換成格式化的字符串以及反過來從格式化的字符串轉(zhuǎn)換成指定的數(shù)據(jù)類型。

to_char 函數(shù)第一個(gè)參數(shù)是待格式化的值,而第二個(gè)是定義輸出或輸出格式的模板。

函數(shù)描述例子返回類型結(jié)果
to_char(timestamp, text)把時(shí)間戳轉(zhuǎn)換成字串select to_char(current_timestamp, ‘HH12:MI:SS’);text06:03:19
to_char(interval, text)把時(shí)間間隔轉(zhuǎn)為字串select to_char(interval ‘14h 6m 20s’, ‘HH24:MI:SS’);text14:06:20
to_date(text, text)把字串轉(zhuǎn)換成日期select to_date(‘25 Jul 2023’, ‘DD Mon YYYY’);date2023-07-24
to_timestamp(text, text)把字串轉(zhuǎn)換成時(shí)間戳select to_timestamp(‘25 Jul 2023’, ‘DD Mon YYYY’);timestamp2023-07-24 00:00:00.000000 +00:00
to_timestamp(double)把UNIX紀(jì)元轉(zhuǎn)換成時(shí)間戳select to_timestamp(1690179888);timestamp2023-07-24 06:24:48.000000 +00:00

用于日期/時(shí)間格式化的模式:

模式描述
HH一天的小時(shí)數(shù)(01-12)
HH12一天的小時(shí)數(shù)(01-12)
HH24一天的小時(shí)數(shù)(00-23)
MI分鐘(00-59)
SS秒(00-59)
MS毫秒(000-999)
US微秒(000000-999999)
AM正午標(biāo)識(shí)(大寫)
Y,YYY帶逗號(hào)的年(4和更多位)
YYYY年(4和更多位)
YYY年的后三位
YY年的后兩位
Y年的最后一位
MONTH全長(zhǎng)大寫月份名(空白填充為9字符)
Month全長(zhǎng)混合大小寫月份名(空白填充為9字符)
month全長(zhǎng)小寫月份名(空白填充為9字符)
MON大寫縮寫月份名(3字符)
Mon縮寫混合大小寫月份名(3字符)
mon小寫縮寫月份名(3字符)
MM月份號(hào)(01-12)
DAY全長(zhǎng)大寫日期名(空白填充為9字符)
Day全長(zhǎng)混合大小寫日期名(空白填充為9字符)
day全長(zhǎng)小寫日期名(空白填充為9字符)
DY縮寫大寫日期名(3字符)
Dy縮寫混合大小寫日期名(3字符)
dy縮寫小寫日期名(3字符)
DDD一年里的日子(001-366)
DD一個(gè)月里的日子(01-31)
D一周里的日子(1-7;周日是1)
W一個(gè)月里的周數(shù)(1-5)(第一周從該月第一天開始)
WW一年里的周數(shù)(1-53)(第一周從該年的第一天開始)

示例:

-- 查詢今天是今年的第幾天
select to_char(now(), 'DDD'); -- 205

擴(kuò)展

查詢某個(gè)日期是否在某段日期范圍,可以使用 > 和 < 判斷;如果使用了 between ,則前一個(gè)日期必須小于后一個(gè)日期。

示例:

-- between 錯(cuò)誤用法
select date '2023-07-24' between date('2023-07-25') - 1 and date('2023-07-25') - 7; -- false
-- between 正確用法
select date '2023-07-24' between date('2023-07-25') - 7 and date('2023-07-25') - 1; -- true

總結(jié) 

到此這篇關(guān)于PostgreSQL設(shè)置時(shí)區(qū)、時(shí)間/日期函數(shù)匯總的文章就介紹到這了,更多相關(guān)PostgreSQL設(shè)置時(shí)區(qū)時(shí)間內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • postgresql查詢今天、昨天、本周、本月、上月、今年、去年的時(shí)間以及計(jì)算時(shí)間之差

    postgresql查詢今天、昨天、本周、本月、上月、今年、去年的時(shí)間以及計(jì)算時(shí)間之差

    PostgreSQL提供了許多返回當(dāng)前日期和時(shí)間的函數(shù),下面這篇文章主要給大家介紹了關(guān)于postgresql查詢今天、昨天、本周、本月、上月、今年、去年的時(shí)間以及計(jì)算時(shí)間之差的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-08-08
  • PostgreSQL?10分區(qū)表及性能測(cè)試報(bào)告小結(jié)

    PostgreSQL?10分區(qū)表及性能測(cè)試報(bào)告小結(jié)

    PostgreSQL的分區(qū)表跟先前版本一樣,也要先建立主表,然后再建立子表,使用繼承的特性,但不需要手工寫規(guī)則了,目前支持range、list分區(qū),10正式版本發(fā)布時(shí)不知會(huì)不會(huì)支持其它方法,感興趣的朋友跟隨小編一起看看吧
    2022-01-01
  • PostgreSQL數(shù)據(jù)庫(kù)字符串拼接、大小寫轉(zhuǎn)換以及substring詳解

    PostgreSQL數(shù)據(jù)庫(kù)字符串拼接、大小寫轉(zhuǎn)換以及substring詳解

    在日常工作中會(huì)遇到將多行的值拼接為一個(gè)值展現(xiàn),下面這篇文章主要給大家介紹了關(guān)于PostgreSQL數(shù)據(jù)庫(kù)字符串拼接、大小寫轉(zhuǎn)換以及substring的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-04-04
  • Vcenter清理/storage/archive空間的處理方式

    Vcenter清理/storage/archive空間的處理方式

    通過SSH登陸到Vcenter并檢查/storage/archive目錄發(fā)現(xiàn)占用過高,該目錄用于存儲(chǔ)歸檔的日志文件和歷史數(shù)據(jù),解決方案是保留近30天的歸檔文件,這篇文章主要給大家介紹了關(guān)于Vcenter清理/storage/archive空間的處理方式,需要的朋友可以參考下
    2024-11-11
  • PostgreSQL查看數(shù)據(jù)庫(kù)占用空間大小的幾種常用方法

    PostgreSQL查看數(shù)據(jù)庫(kù)占用空間大小的幾種常用方法

    在PostgreSQL中,查看數(shù)據(jù)庫(kù)及數(shù)據(jù)表當(dāng)前數(shù)據(jù)的占用量可以通過執(zhí)行特定的SQL查詢來實(shí)現(xiàn),本文給大家介紹了幾種常用的方法,并通過代碼示例講解的非常詳細(xì),需要的朋友可以參考下
    2024-05-05
  • PostgreSQL排查連接鎖問題的常用SQL語句

    PostgreSQL排查連接鎖問題的常用SQL語句

    正常情況下,PostgreSQL只要連上了就能愉快地使用了,但是在一些特別的場(chǎng)景,如壓測(cè)或者某些不可描述的異常,會(huì)出現(xiàn)數(shù)據(jù)庫(kù)連接異常的情況,比如連接數(shù)占滿了,所以本文給大家介紹了PostgreSQL排查連接鎖問題的常用SQL語句,需要的朋友可以參考下
    2024-04-04
  • postgresql 存儲(chǔ)函數(shù)調(diào)用變量的3種方法小結(jié)

    postgresql 存儲(chǔ)函數(shù)調(diào)用變量的3種方法小結(jié)

    這篇文章主要介紹了postgresql 存儲(chǔ)函數(shù)調(diào)用變量的3種方法小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • postgreSQL 數(shù)字與字符串類型轉(zhuǎn)換操作

    postgreSQL 數(shù)字與字符串類型轉(zhuǎn)換操作

    這篇文章主要介紹了postgreSQL 數(shù)字與字符串類型轉(zhuǎn)換操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • PostgreSQL limit的神奇作用詳解

    PostgreSQL limit的神奇作用詳解

    這篇文章主要介紹了PostgreSQL limit的神奇作用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2022-09-09
  • PostgreSQL使用MySQL外表的步驟詳解(mysql_fdw)

    PostgreSQL使用MySQL外表的步驟詳解(mysql_fdw)

    這篇文章主要介紹了PostgreSQL使用MySQL外表的步驟(mysql_fdw),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01

最新評(píng)論