LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-21-2006, 05:09 AM   #1
Reegz
Member
 
Registered: Mar 2006
Location: South Africa
Distribution: Ubuntu, Slackware, openSuse
Posts: 44

Rep: Reputation: 15
Regex in sql


Hi guys,

I am trying to extract certain data from a text column in postgres. I need to retrieve only the data between

Code:
<skuCde>8475394</skuCde>
I need "8475394"

How do i extract this using a sql statement??

Thanks
 
Old 07-21-2006, 06:00 AM   #2
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
select substring (...,'<regexp here, with parenthesis around the part you want>');
 
Old 07-21-2006, 08:01 AM   #3
Reegz
Member
 
Registered: Mar 2006
Location: South Africa
Distribution: Ubuntu, Slackware, openSuse
Posts: 44

Original Poster
Rep: Reputation: 15
Ok, so i'v got this bit working. The problem now is that if i have multiple occurences of skuCde, the output i get is everything between the first opening tag and the last closing tag.

Code:
SELECT aud_id, substring(aud_xml, '<skuCde>\(.*\)</skuCde>' ) from audit where aud_id = 3413;
I've tried adding
Code:
*?
the non-greedy param but then too, i only the first occurence.

Code:
SELECT aud_id, substring(aud_xml, '<skuCde>\(.*?\)</skuCde>' ) from audit where aud_id = 3413;
What am i missing here??
 
Old 07-21-2006, 08:18 AM   #4
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
Are you sure you store right data in DB? Maybe you'll have to write stored procedure - or not store XML in DB.
 
Old 07-21-2006, 08:35 AM   #5
Reegz
Member
 
Registered: Mar 2006
Location: South Africa
Distribution: Ubuntu, Slackware, openSuse
Posts: 44

Original Poster
Rep: Reputation: 15
yeah, the data is correct. Im just trying to find a way to do this in a script so that i dont have to write a java class to do it.

thanks for your help, i think i'm almost near a solution
 
Old 07-21-2006, 08:43 AM   #6
Reegz
Member
 
Registered: Mar 2006
Location: South Africa
Distribution: Ubuntu, Slackware, openSuse
Posts: 44

Original Poster
Rep: Reputation: 15
Heres my solution

psql db9831 -t -c "SELECT aud_xml from audit where aud_id = 3413;" > output;
xmllint --recover -format output > output2;
cat output2 | grep \<sku\> >> output3
 
Old 07-21-2006, 08:59 AM   #7
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
Well done. I new you'll have to make something else parse XML. When I said right data, I didn't mean that data is corrupted, I meant that if XML lies in SQL DB it is suspicious - XML is some structure and DB is some structure, and they can be a bit not suitable for each other.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
.NET regex \b but not / rjwilmsi Programming 0 06-21-2006 03:45 AM
regex help siyisoy Programming 4 04-07-2006 05:32 AM
Regex Help cmfarley19 Programming 5 03-31-2005 10:13 PM
Migrating from MS-SQL server to My-SQL emailssent Linux - Networking 2 02-07-2005 02:20 PM
Which SQL is suitable for EMbedded SQL on C? hk_michael Programming 4 01-10-2005 05:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration