SQL Server: Querying XML fields – XPath, XQuery

If you can remember the syntax you are better than me, but here is an example I crafted for querying XML in SQL Server:

SELECT
T.X.query('declare namespace N="http://helpmenow";//N:me[@now="false"]').value('.', 'NVARCHAR(MAX)')
FROM
(
SELECT
CONVERT(XML, '<help xmlns="http://helpmenow"><me now="true">yay</me><me now="false">too bad</me></help>') X
) T

Of course the result will be “too bad”.
Once you have the value of a field like that it is a normal string, so could be used in a where criteria etc.

See also MSDN – XML Support in Microsoft SQL Server 2005

Advertisements

One response to “SQL Server: Querying XML fields – XPath, XQuery

  1. Pingback: SQL Server: Updating XML fields – XPath, XQuery – #sql #sqlserver #xml #xpath #xquery | Tim's cycling blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s