0

The goal here is to extract an attribute value from a SQL Server 2008 table column. The column is an XML data type, of course, named 'ProductName'. Here's the sample data

<locale en-US="My Text" />

And here's my latest attempt at getting to 'My Text'

SELECT ProductName.value('/@locale en-US', 'nvarchar(max)')   
AS ProductName FROM MyTable

Thanks and good luck!

3 Answers 3

1
SELECT ProductName.value('(/locale/@en-US)[1]', 'nvarchar(max)') AS ProductName 
FROM MyTable
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

SELECT
    ProductName.value('(/locale/@en-US)[1]', 'varchar(50)')
FROM 
    dbo.MyTable

The /locale part of the XPath expression matches the <locale> element, while the @en-US part matches the en-US attribute of that XML element.

Comments

0
declare @xml xml =
'<root>
  <locale en-US="My Text" />
</root>'

select @xml.value('(/root/locale/@en-US)[1]', 'nvarchar(max)')

Result

My Text

Comments

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.