我的表中有一个非传统的JSON列: 这是可能的值:
[{"tarif pour 1 :":"$40,00"},{"Réduction :":"-$10,00 "}]
[{"Full Fare For 10 :":"$50.00"},{"Promotion:":"-$20.00"}]
我的目标是使用postgresql
函数substring
如何获取值
$40,00
$50.00
我可以这样做,但使用2个单独的REGEX,如:
SELECT
, substring(column, '"Full [Ff]are [Ff]or.+?:":"(.+?)"') AS value_1
, substring(column, '"[Pp]lein [Tt]arif [Pp]our.+?:":"(.+?)"') AS value_2
FROM table
如何在我的列中包含两个REGEX以显示其后的美元金额或以[Pp]lein [Tt]arif [Pp]our.+?:":"
或[Ff]ull [Ff]are [Ff]or.+?:":"
答案 0 :(得分:1)
使用coalesce()
:
with a_table(a_column) as (
values
('[{"Plein tarif pour 1 :":"$40,00"},{"Réduction :":"-$10,00 "}]'),
('[{"Full Fare For 10 :":"$50.00"},{"Promotion:":"-$20.00"}]')
)
select
coalesce(
substring(a_column, '"Full [Ff]are [Ff]or.+?:":"(.+?)"'),
substring(a_column, '"[Pp]lein [Tt]arif [Pp]our.+?:":"(.+?)"')
) as value
from a_table;
value
--------
$40,00
$50.00
(2 rows)