Maharshi
Maharshi

Reputation: 1198

Selecting first element from the xml in the column

I have following content in the column. (not a proper xml content)

<menu=REDACTED - contact info>
  <fillcolor=black>
  <border=1>
  <color=white>
  <FontHeight=19>
  <type=text>

Fiddle

I want to fetch a very first element from the xml content which is "menu" as following.

<menu=REDACTED - contact info>

"xmlpath" does not help here as there is no closing tag for any XML elements. I know it is not a proper XML content but I have to figure out the way for the above XML as I am playing around with the existing system.

Question: How can I fetch <menu=REDACTED - contact info> from the whole content?

Upvotes: 0

Views: 156

Answers (1)

404
404

Reputation: 8562

I may be misunderstanding the question but I'm assuming you're saying you want the the first menu element, rather than the first element regardless of its name?

Since it's invalid xml, you'll want to use something like regex:

SELECT (REGEXP_MATCHES(str, '<menu=.*?>'))[1] FROM tes;

That will get the first menu element. See example https://www.db-fiddle.com/f/4jyoMCicNSZpjMt4jFYoz5/1

Upvotes: 1

Related Questions