我有一张桌子,如下表
+----+----------------------------------+
| id | Created_at |
+----+----------------------------------+
| 1 | Monday 8 April 2019 8:30 AM |
| 2 | Tuesday 9 April 2019 5:30 PM |
| 3 | Wednesday 9 April 2019 8:30 PM |
| 4 | Thursday 10 April 2019 11:30 AM |
| 5 | Friday 11 April 2019 10:45 AM |
+----+----------------------------------+
如何获得下表
+----+------------+
| id | Created_at |
+----+------------+
| 1 | 2019-04-08 |
| 2 | 2019-04-09 |
| 3 | 2019-04-09 |
| 4 | 2019-04-10 |
| 5 | 2019-04-11 |
+----+------------+
基本上我想将我的Created_at
列转换为YYYY-MM-DD
格式。
答案 0 :(得分:2)
从示例数据中,您可以尝试使用::DATE
查询#1
SELECT Created_at::DATE
FROM T;
| created_at |
| ------------------------ |
| 2019-04-08T00:00:00.000Z |
| 2019-04-09T00:00:00.000Z |
| 2019-04-09T00:00:00.000Z |
| 2019-04-10T00:00:00.000Z |
| 2019-04-11T00:00:00.000Z |
答案 1 :(得分:1)
PostgreSQL的日期/时间转换非常灵活,应该能够处理您的格式:
WITH
data(Created_at) AS (VALUES
('Monday 8 April 2019 8:30 AM'),
('Tuesday 9 April 2019 5:30 PM'),
('Wednesday 9 April 2019 8:30 PM'),
(' Thursday 10 April 2019 11:30 AM'),
('Friday 11 April 2019 10:45 AM ')
)
SELECT trim(both from Created_at)::date AS Created_at
FROM data;
...导致:
created_at
------------
2019-04-08
2019-04-09
2019-04-09
2019-04-10
2019-04-11
(5 rows)