user6297338
user6297338

Reputation: 11

Parsing XML in SQL, atypical attribute format

This XML format is a given (it comes from an app my company runs):

<User display="User">NAME1</User>

So I've been running the following code to try and tease out the true value (NAME1) from this format:

declare @xml xml = '<User display="User">NAME1</User>'
select @xml.value('(User/@display)[1]', 'nvarchar(max)') as USER_NM

I'm using SQL Server 2012. But nothing I try can pick out the NAME1, rather than User. Any ideas?

Upvotes: 1

Views: 21

Answers (1)

marc_s
marc_s

Reputation: 754508

Just use this snippet instead:

SELECT @xml.value('(User)[1]', 'nvarchar(max)') as USER_NM

This will read out the textual value of the <User> element - NAME1 in your case

Upvotes: 1

Related Questions