SQL Server XQuery: .value() method
Back to XQuery; the third method I wanted to discuss in this series is the .value() method. Like the .query() method, .value() bridges the SQL Server database engines and XQuery engines by extracting information from an XML document. The difference is that while .query() returns XML, .value() translates the specific information requested into a scalar SQL value. For example:
DECLARE @T TABLE ( ID INT, x XML ) INSERT INTO @T ( ID, x ) VALUES ( 1, '<root><plant><fruit>tomato</fruit></plant></root>' ) , ( 2, '<root><plant><fruit>banana</fruit></plant></root>' ) , ( 3, '<root><plant><vegetable>tomato</vegetable></plant></root>' ) SELECT ID, x.value('(.)', 'varchar(max)') FROM @T t
Note that the .value() method has two parameters, an XQuery statement and a string representing the expected SQL Server type for the scalar value returned. The results are as follows:
So, what is the XQuery statement requesting? The axis is referring to the self-node of the XML fragment (and remember that in SQL ServerвЂ™s implementation of XQuery, the query begins at the top of the document), there is no node test (so any node will satisfy the test), and the predicate is referring to the first position  of the path. The .value() function returns the value found at that location, and converts it to a sql value of type varchar(max). So what happens if thereвЂ™s more than one value at a given XPath?
INSERT INTO @T ( ID, x ) VALUES ( 4, '<root><plant><fruit>tomato</fruit><fruit>banana</fruit><vegetable>tomato</vegetable></plant></root>' ) SELECT ID, x.value('(.)', 'varchar(max)') FROM @T t
The above script inserts a slightly more complex XML document than used before; there are now multiple nodes under the <plant> node (in addition to the three rows we already have); the output looks like:
Note how row 4 has a single string that represents all of the values of the XML document. Without a specific XQuery, the value() function returns the values that satisfy the request; in this case, all of them. In the case where the XQuery cannot be satisfied, .value() will return a NULL value to the SQL Server engine, as illustrated by the following code examples:
SELECT ID, x.value('(//plant/fruit)', 'varchar(max)') FROM @T t SELECT ID, x.value('(//plant/fruit)', 'varchar(max)') FROM @T t
The output should look like:
In the first query, we are looking for an axis of any node in the document, a node test of fruit under the <plant> node, and a positional predicate of 1. Rows 1,2, and 4 satisfy this query; the XML document in row 3 doesnвЂ™t have a <fruit node>, and thus returns a NULL. The second query is similar in that several of the XML documents in our table donвЂ™t satisfy the query; only the document in row 4 has node in the second  position of the <fruit> node.
- The .value() function allows you to specify an XQuery statement against an XML datatype looking for a specific value, and
- that returned value is cast to a SQL datatype.
Next up: .nodes()