Casts between XML schema data types
You can use data type constructor functions to cast a value to a specific data type. Specify the value that you want to cast and the type to which you want to cast it.
The following table lists the compatible types for casting. You can cast values only of the listed input types to each target type.
Target type | Source type | Comments |
---|---|---|
xs:untypedAtomic | Any type | |
xs:string | Any type |
|
xs:boolean | xs:untypedAtomic, xs:string, xs:boolean, xs:double, xs:decimal, xs:integer |
|
xs:dayTimeDuration | xs:duration, xs:untypedAtomic, xs:string | A cast from xs:duration to xs:dayTimeDuration results in information loss. To avoid information loss, cast the xs:duration value to an xs:yearMonthDuration value and an xs:dayTimeDuration value and work with both values. |
xs:decimal | Numeric types, xs:untypedAtomic, xs:string, xs:boolean | Values of numeric types are converted to a value that is within the set of possible values for type xs:decimal and is numerically closest to the source. If two values are equally close, the one that is closest to zero is chosen. The source value cannot be +INF, -INF, NaN, or outside of the range of type xs:decimal. For values of type xs:boolean, true is converted to 1.0, and false is converted to 0.0. |
xs:double | Numeric types, xs:untypedAtomic, xs:string, xs:boolean | If the source is of type xs:decimal, or xs:integer, the cast is performed as xs:double(SV cast as xs:string) where SV is the source value. If the source is of type xs:boolean, true is cast to a value of 1.0E0, and false is cast to a value of 0.0E0. |
xs:duration | xs:dayTimeDuration, xs:yearMonthDuration, xs:untypedAtomic, xs:string |
|
xs:integer | Numeric types, xs:untypedAtomic, xs:string, xs:boolean | If the source type is a numeric type other than integer, the result is the source value with the fractional part discarded. The source cannot be outside of the range of type xs:integer. For values of type xs:boolean, true is converted to 1, and false is converted to 0. |
xs:date | xs:dateTime, xs:untypedAtomic, xs:string | The time portion of the source value is not used in the conversion. |
xs:time | xs:dateTime, xs:untypedAtomic, xs:string | The date portion of the source value is not used in the conversion. |
xs:dateTime | xs:date, xs:untypedAtomic, xs:string | The time portion of the target value is the first moment of the day. The value is not adjustment for the time zone. |
xs:yearMonthDuration | xs:duration, xs:untypedAtomic, xs:string | A cast from xs:duration to xs:yearMonthDuration results in information loss. To avoid information loss, cast the xs:duration value to an xs:yearMonthDuration value and an xs:dayTimeDuration value and work with both values. |
Example
The following XQuery expression returns purchase orders that contain more than one item. The xs:integer constructor function casts the value of the quantity element to an integer. That integer can then be compared to the integer 1.declare namespace ipo="http://www.example.com/IPO";
/ipo:purchaseOrder[items/item/quantity/xs:integer(.) > 1]
If
the result of the path expression is not explicitly cast to an integer, Db2 converts both operands to type xs:double to make the numeric comparison.
The cast ensures that the values are compared as values of type xs:integer.