0

I have a column in my table called data_xml that has records presented like this-

<data user_id="A" user_id_type="1" date="2019-10-31"/>
<data user_id="B" user_id_type="2" date="2020-01-30"/>

I want to be able to pull user_id_type records from the column. I tried using split_part but because the xml data doesnot have a delimiter as seen above I am not sure how to proceed.

Does anyone have a better alternative.

1 Answer 1

4

You can use xpath() for that:

select (xpath('/data/@user_id_type', data_xml::xml))[1]
from the_table;

xpath() returns a array of all matches, that's why the [1] is needed.

Sign up to request clarification or add additional context in comments.

5 Comments

i get the following error when I try the above in DBviz - function xpath(unknown, character varying) does not exist. Jint: no function matches the given name and argument type. you may need to add explicit type casts. Not sure why?
Also I need to pull all the user_id_type. Should I have [2] instead of [1] ?
Then apparently you don't have a xml but a varchar column - you need to cast it to xml. Use @user_id_type if you want that attribte
thanks a_horse_with_no_name How do I cast varchar to xml using the xpath() function
Thank you @a_horse_with_no_name

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.