简体   繁体   中英

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.

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 ":"

Because there is the number i'm looking for.

Do you have an idea?

Output should be

  • 1
  • 1782
  • 17

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"

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:

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

you can use the unest with ordinally give rownumber and filter with it

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:

row_number  t
1   1
2   1782
3   17

Found it out:)

split_part(extradata,'"',8)

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