Skip to Main Content
  • Questions
  • How can i extract data from a URL string in SQL?

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, KARIM.

Asked: November 07, 2019 - 3:34 pm UTC

Last updated: November 11, 2019 - 1:25 am UTC

Version: 2012

Viewed 10K+ times! This question is

You Asked

Hi TOM, i am trying to extract some data from a URL and running into some issues. The URL's will look something like this:

http://192.168.201.242:8000/express_get?time=$startDate&from=$phoneNumber&to=$CAMPAIGN&Id_agent=$idExternalUser&spent=$duration&qualifId=$test&compagneId=$CAMPAIGN


I am trying to return back the column name and data corresponding to it.
(E.g): time = $startDate
from = $phoneNumber

Any suggestions as how to do this properly?

and Connor said...

Check out URL_NINJA written by a friend of mine Morten Egan.

https://github.com/morten-egan/url_ninja

But if you want SQL, here's some samples

1) Get the pairs

SQL> with t as
  2  ( select 'http://192.168.201.242:8000/express_get?time=XXXXX&from=YYYYY&to=JJJJJ&Id_agent=LLLLLL&spent=1234&qualifId=HHHHH&compagneId=UUUUUU' x from dual )
  3  ,
  4  params as
  5  ( select '&'||substr(x,instr(x,'?')+1) p from t )
  6  select
  7     regexp_substr (p, '[^&]+', 1, level)
  8  from params
  9  connect by level <= regexp_count(p,'&');

REGEXP_SUBSTR(P,'[^&]+',1,LEVEL)
----------------------------------------------------------------------------------------------------------------------------------
time=XXXXX
from=YYYYY
to=JJJJJ
Id_agent=LLLLLL
spent=1234
qualifId=HHHHH
compagneId=UUUUUU


2) Get the key/values

SQL> with t as
  2  ( select 'http://192.168.201.242:8000/express_get?time=XXXXX&from=YYYYY&to=JJJJJ&Id_agent=LLLLLL&spent=1234&qualifId=HHHHH&compagneId=UUUUUU' x from dual )
  3  ,
  4  params as
  5  ( select '&'||substr(x,instr(x,'?')+1) p from t ),
  6  pairs as (
  7  select
  8     regexp_substr (p, '[^&]+', 1, level) pair
  9  from params
 10  connect by level <= regexp_count(p,'&')
 11  )
 12  select
 13    substr(pair,1,instr(pair,'=')-1) p1,
 14    substr(pair,instr(pair,'=')+1) p2
 15  from pairs;

P1                   P2
-------------------- --------------------
time                 XXXXX
from                 YYYYY
to                   JJJJJ
Id_agent             LLLLLL
spent                1234
qualifId             HHHHH
compagneId           UUUUUU


Rating

  (2 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

KARIM HAYANI, November 08, 2019 - 8:06 am UTC

thank you, you are a lifesaver
Connor McDonald
November 11, 2019 - 1:24 am UTC

glad we could help

KARIM HAYANI, November 08, 2019 - 10:09 am UTC

Hi Connor! Thanks for your thoughtful response. I really appreciate it!

what if i want to insert the data from the URL in a Table T instead
Connor McDonald
November 11, 2019 - 1:25 am UTC

Then you wrap it in an insert no?

insert into MY_TABLE
with .....

More to Explore

SQL

The Oracle documentation contains a complete SQL reference.