简体   繁体   中英

JSON extract multiple columns PostgreSQL

I had a question earlier: PostgreSQL trim text field with regex (or else) And I got a wonderful answer by a_horse_with_no_name. Now I have an additional question regarding this issue.

So here it is this rextester https://rextester.com/SUWG96428 and the goal is to have all the ids in a separate column. Is it possible at all?

Like this:

+---+----+-------+-------+
|   | id | ids_1 | ids_2 |
+---+----+-------+-------+
| 1 |  1 |  4202 |  4203 |
| 2 |  2 |  4204 |       |
| 3 |  3 |  4201 |       |
+---+----+-------+-------+

Yep, you can modify your query like:

select 
    t.id, 
    right(((the_column::json->'itemID')->>0)::varchar, 4) as col1,
    right(((the_column::json->'itemID')->>1)::varchar, 4) as col2,
    right(((the_column::json->'itemID')->>2)::varchar, 4) as col3
from the_table t;

DB Fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM