开发者

How to query the xml in SQL XML on a SQL Server 2008 database

I have a SQL Server 2008 database. There is a table called Documents with the following schema:

 Id            int     PK      
 DocumentXml   xml

The xml documents开发者_运维百科 all look something like:

<docroot>
    <name>Some Name</name>
</docroot>

I want to select all the records where the text value of /docroot/name begins with an "S" (case-insenstive).

How do I execute this query with the best performance?


Not sure about the performance - but you can do something like this:

SELECT (list of columns)
FROM dbo.Documents
WHERE DocumentXml.value('(/docroot/name)[1]', 'varchar(100)') LIKE 'S%'

If your database collation is case-insensitive, this LIKE operation will be case-insensitive, too.


I want to select all the records where the text value of /docroot/name begins with an "S" (case-insenstive).

Use this XPath expression:

/*/name[starts-with(translate(.,'S','s'), 's')]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜