用PostgreSQL子串多行

时间:2019-03-05 15:52:09

标签: postgresql

我只有一个文本列具有这种类型的值

---
cart_id: 23332251
id: 1824
push: 
key: 
last_checked_in_at: 2019-03-04 10:39:53.946659000 Z
other_field: hello

我想创建一个仅包含last_checked_in_at的列,并将值提取为2019-03-04 10:39:53.946659000

我尝试过

SELECT substring(object FROM '.*last_checked_in_at: (.*) Z') AS last_checkin_at, *
FROM versions;

但是我恳求这个领域,但是它永远不会停止,并占用下面的其余行。

我尝试了substring(object FROM '.*last_checked_in_at: (.*) Z$'),但得到NULL。我在PostgreSQL 10上。

1 个答案:

答案 0 :(得分:1)

给予

CREATE TABLE test (
    object text
);
INSERT INTO test VALUES
($$---
cart_id: 23332251
id: 1824
push: 
key: 
last_checked_in_at: 2019-03-04 10:39:53.946659000 Z
other_field: hello$$);

SQL

SELECT substring(object FROM '.*last_checked_in_at: (.*) Z') AS last_checkin_at, * FROM test;

收益

+-------------------------------+-----------------------------------------------------+
|        last_checkin_at        |                       object                        |
+-------------------------------+-----------------------------------------------------+
| 2019-03-04 10:39:53.946659000 | ---                                                +|
|                               | cart_id: 23332251                                  +|
|                               | id: 1824                                           +|
|                               | push:                                              +|
|                               | key:                                               +|
|                               | last_checked_in_at: 2019-03-04 10:39:53.946659000 Z+|
|                               | other_field: hello                                  |
+-------------------------------+-----------------------------------------------------+

因此substring(object FROM '.*last_checked_in_at: (.*) Z')可以按需工作。 如果您不想第二列,请使用

SELECT substring(object FROM '.*last_checked_in_at: (.*) Z') AS last_checkin_at FROM test;