在本指南中,您将了解如何将 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 值。
让我们在这个表中插入一些数据。
中找到本指南中使用的所有 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 设置。中找到更多信息。
我们可以从这个表中选择并查看结果。
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/