0

I have one text column that have this type of value

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

I want to have a column with only last_checked_in_at and extract the value as 2019-03-04 10:39:53.946659000

I tried

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

But I get the begging of the field but it never stop and take the rest of the lines below.

I tried substring(object FROM '.*last_checked_in_at: (.*) Z$') but I get NULL. I am on PostgreSQL 10.

1
  • I succeed by having a dedicated regex substring(object FROM '(\d*-\d*-\d* \d*:\d*:\d*\.\d*)') but I would to understand why it is not working with the one bellow. Commented Mar 5, 2019 at 16:01

1 Answer 1

1

Given

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$$);

the SQL

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

yields

+-------------------------------+-----------------------------------------------------+
|        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                                  |
+-------------------------------+-----------------------------------------------------+

So substring(object FROM '.*last_checked_in_at: (.*) Z') works as desired. If you don't want the second column, use

SELECT substring(object FROM '.*last_checked_in_at: (.*) Z') AS last_checkin_at FROM test;
Sign up to request clarification or add additional context in comments.

3 Comments

But it is the case?
My mistake. I tested .*last_checked_in_at: (.*) Z and I think it should work as desired. I've edited the post above accordingly.
If this is not the behavior you are seeing with your actual data, then please post SELECT object::bytea FROM versions LIMIT 3. Given the values as bytes we might be able to reproduce the behavior you are seeing. We would be able to see if there is a non-visible character that is not accounted for in the regex, for example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.