sql - Postgresql substring to extract between angle brackets and quotes -


I have an area in which xml is I am trying to extract the data from an attribute which is Gt; which remains at the end of the beginning tag of an element.

Example

I need 250 values.

  & lt; Request id = "1111" "order =" "123456" scale = "" 250 "" & gt; & Lt; Project file = "test.txt" / / gt; & Lt; / Request & gt;  

I want to remove it using this substring. So I did:

  select substring from mytable (<% scale = "#"% # "" & gt; / Code>   

But the problem is that the next element for the other record is & lt; Pro does not start

So I tried it

  from mytable substring ('% scale' 'from # "% #" to' '' For '' 'for'% ') / Code>  

It gives:

  250 "& gt; Project file = "test.txt" /> & Lt; / Request & gt;  

Where am I going? Is there a better way? thanks a lot.

If you are always sure that you type in scale attribute in If the requests are made after the element, then it can work for you (I have tried this only on PG 9.3.5):

  SELECT (regexp_matches (myxml, ' & Lt; request [^ & gt;] *? \\ + scale = "" (\ d +) "" ')) [1] AS scale from my scale;  

Some inappropriate fake data (I have variation in both sca values ​​and internal element names) as a display:

 < Code> postgres = # SELECT * mytable; Myxml ------------------------------------------------- ------------------------------------------ & lt; Request id = "1111" command = "" 123456 "" scale = "" 250 "" & gt; Project file = "test.txt" "/ /> gt; Request & gt; & Lt; Request id = "1111" "order =" "123456" "scale =" "250" "& gt; & lt; not proof file =" "test.txt" "/ /> gt; request & gt; & lt; request Id = "1111" "order =" "123456" "scale =" "253" "& gt; & Lt; Not proof file = "" test.txt "" / /> gt; Request & gt; (3 lines) Postgres = # SELECT (regexp_matches (myxml, '& lt; request [^ & gt;] *? \ S + scale = "" (\ d +) ""')) [1] Scale; Scale ------- 250 250 253 (3 lines)  

For special points of the value of the regular expression I used to be the scale Hope to join If your data can be different from that, then the capture part should be like (\ d +) instead of ([^ "] +) .


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -