在本指南中,您將了解如何將 DATE 值格式化為多種不同的格式,如何將 DATE 值插入到表中,等等。
概括
您可以在Postgres中以多種不同格式將日期值插入 DATE 列,但推薦的格式是 YYYY-MM-DD 的 ISO 格式。
當您使用 TO_CHAR 函數顯示日期時,您可以在 Postgres 中格式化日期,例如 TO_CHAR(order_date, 'MM-DD-YYYY')。
Postgres 日期數據類型
我們將在本指南中使用 DATE數據類型。
Postgres 中的 DATE 數據類型捕獲沒有時間分量的日期。
它可以存儲一系列值:從公元前 4,713 年到公元 5,874,897 年。
要定義具有 DATE 數據類型的列,只需指定數據類型:
CREATE TABLE cust_order (
order_id INT,
order_date DATE
);order_date 列存儲為 DATE 值。
讓我們在這個表中插入一些數據。
您可以在我的 GitHub 存儲庫中找到本指南中使用的所有 SQL 腳本。
在表格中插入日期
在許多其他數據庫中,您必須使用某種默認格式插入日期值,或使用函數將輸入格式轉換為所需格式。
但是,在 Postgres 中,您可以插入許多不同類型的日期數據,并且可以正常工作。
推薦的格式是符合 ISO 8601 標準的格式,即 YYYY-MM-DD。
下面是一些在 Postgres 中將數據插入 DATE 列的示例。
INSERT INTO cust_order (order_id, order_date) VALUES (1, '2022-10-13');
INSERT INTO cust_order (order_id, order_date) VALUES (2, '14-OCT-2022');
INSERT INTO cust_order (order_id, order_date) VALUES (3, '20221015');所有這三行都應該毫無問題地插入。
但是,我們可以嘗試運行這個語句:
INSERT INTO cust_order (order_id, order_date) VALUES (4, '16-10-2022');我們會得到一個錯誤:
ERROR: date/time field value out of range: "16-10-2022"
LINE 1: ...INTO cust_order (order_id, order_date) VALUES (4, '16-10-202...
^
HINT: Perhaps you need a different "datestyle" setting.
SQL state: 22008
Character: 58發生這種情況是因為我們指定的格式對數據庫來說不清楚哪個字段是日期,哪個字段是月份。我們通過查看它知道 16 是一天,因為沒有 16 月份的編號。
但是,數據庫不知道這一點。
如錯誤消息中的提示所述,解決此問題的一種方法是使用不同的 DateStyle 設置。您可以在此處的 Postgres 文檔中找到更多信息。
我們可以從這個表中選擇并查看結果。
SELECT order_id, order_date
FROM cust_order;結果 :
| order_id | order_date |
| 1 | 2022-10-13 |
| 2 | 2022-10-14 |
| 3 | 2022-10-15 |
使用 TO_CHAR 以特定格式顯示日期
當我們從表中選擇 DATE 列時,我們可以看到顯示的格式:
SELECT order_id, order_date
FROM cust_order;結果:
| order_id | order_date |
| 1 | 2022-10-13 |
| 2 | 2022-10-14 |
| 3 | 2022-10-15 |
日期值的格式為 YYYY-MM-DD。
如果我們想以不同的格式顯示它怎么辦?
您可以在 Postgres 中使用 TO_CHAR 函數。
TO_CHAR 函數如下所示:
TO_CHAR (date_value, output_format)參數是:
- date_value:您希望以不同格式顯示的值。
- output_format:要顯示的格式
output_format 必須使用單引號并使用下表中的模式。
例如,您的 YYYY 函數中的值在顯示時將被替換為四位數的年份。
這是適用于日期的輸出格式值的列表。
| Pattern | Description |
| Y,YYY | year (4 or more digits) with a comma |
| YYYY | year (4 or more digits) |
| YYY | last 3 digits of year |
| YY | last 2 digits of year |
| Y | last digit of year |
| IYYY | ISO 8601 week-numbering year |
| IYY | last 3 digits of ISO 8601 week-numbering year |
| IY | last 2 digits of ISO 8601 week-numbering year |
| I | last digit of ISO 8601 week-numbering year |
| BC, bc, AD, or ad | era indicator without periods |
| B.C., b.c., A.D. or a.d. | era indicator with periods |
| MONTH | full upper case month name (blank-padded to 9 characters) |
| Month | full capitalised month name (blank-padded to 9 characters) |
| month | full lower case month name (blank-padded to 9 characters) |
| MON | abbreviated upper case month name |
| Mon | abbreviated capitalised month name |
| mon | abbreviated lower case month name |
| MM | month number (01-12) |
| DAY | full upper case day name (blank-padded to 9 characters) |
| Day | full capitalised day name (blank-padded to 9 characters) |
| day | full lower case day name (blank-padded to 9 characters) |
| DY | abbreviated upper case day name (blank-padded to 9 characters) |
| Dy | abbreviated capitalised day name (blank-padded to 9 characters) |
| dy | abbreviated lower case day name (blank-padded to 9 characters) |
| DDD | day of year (001-366) |
| IDDD | day of ISO 8601 week-numbering year (001-371). Day 1 of the year is Monday of the first ISO week |
| DD | day of month (01-31) |
| D | day of the week, Sunday (1) to Saturday (7) |
| ID | ISO 8601 day of the week, Monday (1) to Sunday (7) |
| W | week of the month (1-5). The first week starts on the first day of the month |
| WW | week number of the year (1-53). The first week starts on the first day of the year |
| IW | week number of ISO 8601 week-numbering year (01-53). The first Thursday of the year is in week 1. |
| CC | 2 digit century (the 21st century starts on 2001-01-01) |
| J | Julian Date (number of days since Nov 24, 4714 BC) |
| Q | quarter |
| RM | month in upper case Roman numerals (I-XII, I=January) |
| rm | month in lower case Roman numerals (i-xii, i=January) |
讓我們看一些例子。
示例 1 – DD/MM/YYYY
這是以 DD/MM/YYYY 格式格式化日期的示例。
SELECT
order_date,
TO_CHAR(order_date, 'DD/MM/YYYY') AS formatted_date
FROM cust_order;結果:
| order_date | formatted_date |
| 2022-10-13 | 13/10/2022 |
| 2022-10-14 | 14/10/2022 |
| 2022-10-15 | 15/10/2022 |
示例 2 – 名稱
這是一個使用日期和月份名稱的示例。我們還添加了日期相同部分的兩個示例:Day(日期名稱)和 DD(日期編號)。
SELECT
order_date,
TO_CHAR(order_date, 'Day, DD Month, YYYY') AS formatted_date
FROM cust_order;我們可以看到日期、日期、月份名稱和年份的名稱。
示例 3 – 其他屬性
這是獲取日期的其他屬性的示例。
SELECT
order_date,
TO_CHAR(order_date, 'MM') AS month_num,
TO_CHAR(order_date, 'DDD') AS day_of_year,
TO_CHAR(order_date, 'ID') AS day_of_week,
TO_CHAR(order_date, 'WW') AS week_of_year,
TO_CHAR(order_date, 'J') AS julian_date
FROM cust_order;結果:
我們可以在這里看到日期值的一些不同屬性。
經常問的問題
如何在 PostgreSQL 中將日期轉換為字符串?
您可以使用 TO_CHAR 函數將日期值轉換為字符串。這個函數的輸出是一個字符串。
如上面的示例所示,您可以根據需要定義輸出格式。
如何將日期時間轉換為 PostgreSQL 中的日期?
要將 DATETIME 轉換為 DATE(或刪除時間組件),您可以使用 DATE 函數或 ::DATE 運算符。
您可以將 DATETIME 包含在 DATE 函數中。或者,您可以在 DATETIME 值之后添加 ::DATE。
這是一個使用 NOW() 函數的示例,該函數以 DATETIME 格式返回當前日期和時間。
SELECT
NOW(),
NOW()::DATE,
DATE(NOW());結果:
| now | now | date |
| 2022-10-13 18:28:10.356797+11 | 2022-10-13 | 2022-10-13 |
我們可以在第二列和第三列中看到時間分量已被刪除。
結論
在 Postgres 中格式化日期可以使用非常靈活的 TO_CHAR 函數來完成。您還可以使用多種格式插入日期值,但推薦的格式是 YYYY-MM-DD 的 ISO 格式。
原文標題:How to Format a Date in PostgreSQL
原文作者:Ben Brumm
原文鏈接:https://www.databasestar.com/format-date-postgresql/




