value() Method
XQuery to scalar
What changes
- Input
- xml instance, XQuery singleton, SQL type
- Output or change
- scalar of the SQL type
Syntax at a glance
value ( XQuery , SQLType )
Work example
Read one XML value into a SQL scalar type for a join, filter, or result column.
Cast to the required SQL type and add a singleton predicate such as [1] to the XQuery path.
Check your work value requires an expression that selects at most one value and can fail when conversion is invalid.
Practice SQL
Open in SQL FlowDECLARE @doc xml = N'<Order id="42" />'; SELECT @doc.value('(/Order/@id)[1]', 'int') AS order_id;
Open Microsoft Learn for full syntax, permissions, and details for your version.