PostgreSql 日期时间输出风格及顺序

一、概述

PostgreSQL 中日期时间的输出格式及顺序均由 datestyle 参数控制,两者可以被独立设置或者一起设置。默认值是 ISO,MDY。

二、输出格式

PostgreSQL 中时间/日期类型的输出格式可以设成四种风格之一:ISO 8601、SQL(Ingres)、传统的 POSTGRES(Unix的date格式)或 German 。缺省是 ISO 格式(ISO标准要求使用 ISO 8601 格式)。

sql 复制代码
--ISO 风格
postgres=# set datestyle = 'ISO';
SET
postgres=# select current_timestamp;
       current_timestamp
-------------------------------
 2023-02-27 16:21:40.605044+08
(1 row)

--SQL 风格
postgres=# set datestyle = 'SQL';
SET
postgres=# select current_timestamp;
       current_timestamp
--------------------------------
 02/27/2023 16:22:00.884861 CST
(1 row)

--POSTGRES 风格
postgres=# set datestyle = 'POSTGRES';
SET
postgres=# select current_timestamp;
          current_timestamp
-------------------------------------
 Mon Feb 27 16:22:14.589116 2023 CST
(1 row)

--German 风格
postgres=# set datestyle = 'German';
SET
postgres=# select current_timestamp;
       current_timestamp
--------------------------------
 27.02.2023 16:22:23.080493 CST
(1 row)

三、输入输出顺序

PostgreSQL 中时间输入输出顺序可设置三种顺序之一:DMY、MDY 或 YMD,缺省是 MDY 顺序。

sql 复制代码
--风格为 ISO 时的顺序
postgres=# show datestyle;
 DateStyle
-----------
 ISO, MDY
(1 row)

postgres=# select current_timestamp;
      current_timestamp
------------------------------
 2023-03-06 09:40:51.05446+08
(1 row)

postgres=# set datestyle = 'DMY';
SET
postgres=# select current_timestamp;
       current_timestamp
-------------------------------
 2023-03-06 09:41:24.996177+08
(1 row)

postgres=# set datestyle = 'YMD';
SET
postgres=# select current_timestamp;
       current_timestamp
-------------------------------
 2023-03-06 09:41:35.327119+08
(1 row)

--风格为 SQL 时的顺序
postgres=# set datestyle = 'SQL';
SET
postgres=# show datestyle;
 DateStyle
-----------
 SQL, MDY
(1 row)

postgres=# select current_timestamp;
       current_timestamp
--------------------------------
 03/06/2023 09:45:41.371565 CST
(1 row)

postgres=# set datestyle = 'DMY';
SET
postgres=# select current_timestamp;
       current_timestamp
--------------------------------
 06/03/2023 09:45:48.797223 CST
(1 row)

postgres=# set datestyle = 'YMD';
SET
postgres=# select current_timestamp;
       current_timestamp
--------------------------------
 03/06/2023 09:46:04.377456 CST
(1 row)

--风格为 POSTGRES 时的顺序
postgres=# set datestyle = 'POSTGRES';
SET
postgres=# show datestyle;
   DateStyle
---------------
 Postgres, MDY
(1 row)

postgres=# select current_timestamp;
          current_timestamp
-------------------------------------
 Mon Mar 06 09:48:28.792725 2023 CST
(1 row)

postgres=# set datestyle = 'DMY';
SET
postgres=# select current_timestamp;
         current_timestamp
------------------------------------
 Mon 06 Mar 09:48:43.27396 2023 CST
(1 row)

postgres=# set datestyle = 'YMD';
SET
postgres=# select current_timestamp;
          current_timestamp
-------------------------------------
 Mon Mar 06 09:49:10.084696 2023 CST
(1 row)

--风格为 German 时的顺序
postgres=# set datestyle = 'German';
SET
postgres=# show datestyle;
  DateStyle
-------------
 German, DMY
(1 row)

postgres=# select current_timestamp;
       current_timestamp
--------------------------------
 06.03.2023 09:50:41.023778 CST
(1 row)

postgres=# set datestyle = 'MDY';
SET
postgres=# select current_timestamp;
       current_timestamp
--------------------------------
 06.03.2023 09:50:55.125557 CST
(1 row)

postgres=# set datestyle = 'YMD';
SET
postgres=# select current_timestamp;
       current_timestamp
-------------------------------
 06.03.2023 09:51:16.61567 CST
(1 row)

四、总结

  • 风格为 ISO 和 German 时,时间显示结果不受排序影响。
  • 风格为 SQL 和 POSTGRES 时,时间显示结果中的 月、日 受排序影响。
相关推荐
卜及中15 分钟前
【Redis/2】核心特性、应用场景与安装配置
数据库·redis·缓存
LucianaiB33 分钟前
如何做好一份优秀的技术文档:专业指南与最佳实践
android·java·数据库
Eiceblue1 小时前
Python读取PDF:文本、图片与文档属性
数据库·python·pdf
在未来等你3 小时前
SQL进阶之旅 Day 21:临时表与内存表应用
sql·mysql·postgresql·database·temporary-table·memory-table·sql-optimization
敖云岚4 小时前
【Redis】分布式锁的介绍与演进之路
数据库·redis·分布式
LUCIAZZZ4 小时前
HikariCP数据库连接池原理解析
java·jvm·数据库·spring·springboot·线程池·连接池
我在北京coding4 小时前
300道GaussDB(WMS)题目及答案。
数据库·gaussdb
小Tomkk5 小时前
阿里云 RDS mysql 5.7 怎么 添加白名单 并链接数据库
数据库·mysql·阿里云
明月醉窗台5 小时前
qt使用笔记二:main.cpp详解
数据库·笔记·qt
沉到海底去吧Go6 小时前
【图片自动识别改名】识别图片中的文字并批量改名的工具,根据文字对图片批量改名,基于QT和腾讯OCR识别的实现方案
数据库·qt·ocr·图片识别自动改名·图片区域识别改名·pdf识别改名