You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/t-sql/xml/value-method-xml-data-type.md
+42-6Lines changed: 42 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -38,7 +38,10 @@ The *XQuery* expression, a string literal, that retrieves data inside the XML in
38
38
39
39
#### *SQLType*
40
40
41
-
The preferred SQL type, a string literal, to be returned. The return type of this method matches the *SQLType* parameter. *SQLType* can't be an **xml** data type, a common language runtime (CLR) user-defined type, **image**, **text**, **ntext**, or **sql_variant** data type. *SQLType* can be a user-defined SQL data type.
41
+
The preferred SQL type, a string literal, to be returned. The return type of this method matches the *SQLType* parameter. *SQLType* can be a user-defined SQL data type.
42
+
43
+
> [!NOTE]
44
+
> *SQLType* can't be one of the following data types: **xml**, **image**, **text**, **ntext**, **sql_variant**, or a common language runtime (CLR) user-defined type.
42
45
43
46
The `value()` method uses the [!INCLUDE [tsql](../../includes/tsql-md.md)]`CONVERT` operator implicitly. `value()` tries to convert the result of the XQuery expression, the serialized string representation, from XML Schema Definition (XSD) type to the corresponding SQL type specified by [!INCLUDE [tsql](../../includes/tsql-md.md)] conversion. For more information about type casting rules for `CONVERT`, see [CAST and CONVERT](../functions/cast-and-convert-transact-sql.md).
44
47
@@ -73,7 +76,7 @@ A value of `1` is returned as a result.
73
76
74
77
Although there's only one `ProductID` attribute in the XML instance, the static typing rules require you to explicitly specify that the path expression returns a singleton. Therefore, the `[1]` is added to the end of the path expression. For more information about static typing, see [XQuery and Static Typing](../../xquery/xquery-and-static-typing.md).
75
78
76
-
### B. Use the value() method to retrieve a value from an XML type column
79
+
### B. Use the value() method to retrieve an integer value from an XML type column
77
80
78
81
The following query is specified against an **xml** type column (`CatalogDescription`) in the [!INCLUDE [sssampledbobject-md](../../includes/sssampledbobject-md.md)] database. The query retrieves `ProductModelID` attribute values from each XML instance stored in the column.
79
82
@@ -92,15 +95,48 @@ Note from the previous query:
92
95
93
96
- Per static typing requirements, `[1]` is added at the end of the path expression in the `value()` method to explicitly indicate that the path expression returns a singleton.
### C. Use the value() method to retrieve a string value from an XML type column
110
+
111
+
The following query is specified against the **xml** type column (`CatalogDescription`) in the [!INCLUDE [sssampledbobject-md](../../includes/sssampledbobject-md.md)] database. The query retrieves `ProductModelName` attribute values from each XML instance stored in the column.
(/PD:ProductDescription/@ProductModelName)[1]', 'varchar(50)') AS Result
117
+
FROMProduction.ProductModel
118
+
WHERE CatalogDescription IS NOT NULL
119
+
ORDER BY Result DESC;
120
+
```
121
+
122
+
Note from the previous query:
123
+
124
+
- The `namespace` keyword is used to define a namespace prefix.
125
+
126
+
- Per static typing requirements, `[1]` is added at the end of the path expression in the `value()` method to explicitly indicate that the path expression returns a singleton.
### C. Use the value() and exist() methods to retrieve values from an XML type column
139
+
### D. Use the value() and exist() methods to retrieve values from an XML type column
104
140
105
141
The following example shows using both the `value()` method and the [exist() method](exist-method-xml-data-type.md) of the **xml** data type. The `value()` method is used to retrieve `ProductModelID` attribute values from the XML. The `exist()` method in the `WHERE` clause is used to filter the rows from the table.
106
142
@@ -134,7 +170,7 @@ Here's the partial result:
134
170
...
135
171
```
136
172
137
-
### D. Use the exist() method instead of the value() method
173
+
### E. Use the exist() method instead of the value() method
138
174
139
175
For performance reasons, instead of using the `value()` method in a predicate to compare with a relational value, use `exist()` with `sql:column()`. For example:
0 commit comments