[英]Extract substring from string in postgresql between an end delimiter and its previous motif
i'm really struggling on this one:我真的在这方面苦苦挣扎:
I have some extradata in a postgresql db in which a number matters.我在 postgresql 数据库中有一些额外数据,其中一个数字很重要。
Exemple:示例:
{"code_partenaire":"welcome","sous_statut_abc":"1","statut_alpha":"blabla"}
{"code_partenaire":"code33","sous_statut_def":"1782","statut_alpha":"blablabla"}
{"code_partenaire":"222","sous_statut_ghei":"17","statut_alpha":"blablaa"}
I would like to extract the character between ","statut_alpha"
and its previous ":"
我想提取","statut_alpha"
和它之前的":"
之间的字符
Because there is the number i'm looking for.因为有我要找的号码。
Do you have an idea?你有想法吗?
Output should be Output 应该是
Thanks for your help:)谢谢你的帮助:)
For the output you given, the substring is tedious process, with the conditions (1) before: ","statut_alpha" for this used the reverse string and find the last position of ":" (2) after the last ":" before ","statut_alpha"对于您给出的 output,substring 是一个繁琐的过程,条件 (1) before: ","statut_alpha" 为此使用了反向字符串并找到最后一个 position 之后的 ":" ","statut_alpha"
create table substdata (datac varchar(300));
insert into substdata values ('{"code_partenaire":"welcome","sous_statut_abc":"1","statut_alpha":"blabla"}');
insert into substdata values ('{"code_partenaire":"code33","sous_statut_def":"1782","statut_alpha":"blablabla"}');
insert into substdata values ('{"code_partenaire":"222","sous_statut_ghei":"17","statut_alpha":"blablaa"}');
select
substring
(datac,0,
POSITION('","statut_alpha"' IN datac))
,
length( substring
(datac,0,
POSITION('","statut_alpha"' IN datac)) ), (
length( substring
(datac,0,
POSITION('","statut_alpha"' IN datac)) )
-
pOSITION('":"' in
reverse(
substring
(datac,0,
POSITION('","statut_alpha"' IN datac))
))) as minus
,
substring (substring
(datac,0,
POSITION('","statut_alpha"' IN datac))
,
(
length( substring
(datac,0,
POSITION('","statut_alpha"' IN datac)) )
-
pOSITION('":"' in
reverse(
substring
(datac,0,
POSITION('","statut_alpha"' IN datac))
)))+2,( length( substring
(datac,0,
POSITION('","statut_alpha"' IN datac)) ))) as finresult
from substdata
OUTPUT: OUTPUT:
substring length minus finresult
{"code_partenaire":"welcome","sous_statut_abc":"1 49 47 1
{"code_partenaire":"code33","sous_statut_def":"1782 51 46 1782
{"code_partenaire":"222","sous_statut_ghei":"27 47 44 17
if the first condition is always in the third position in the string如果第一个条件总是在字符串中的第三个 position
you can use the unest with ordinally give rownumber and filter with it你可以使用 unest 通常给出行号并用它过滤
select row_number() over(),t from (
select
row_number() over() as id ,t
from substdata ,
unnest(string_to_array(
substring
(datac,0,
POSITION('","statut_alpha"' IN datac)), '":"'))
WITH ORDINALITY As T ) as drt
WHERE (id % 3) = 0
OUTPUT 2: OUTPUT 2:
row_number t
1 1
2 1782
3 17
Found it out:)发现了:)
split_part(extradata,'"',8)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.