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.

Table 1. Compatible types for casting
Target type Source type Comments
xs:untypedAtomic Any type  
xs:string Any type
  • If the source type is xs:boolean, the result is true or false.
  • If the source type is xs:integer, the result is the canonical lexical representation of the value, as defined in the XML Schema specification.
  • If the source type is xs:decimal:
    • If the value has no significant digits after the decimal point, the decimal point and the zeroes that follow the decimal point are deleted, and the rules for casting from xs:integer apply.
    • Otherwise, the result is the canonical lexical representation of the value, as defined in the XML Schema specification.
  • If the source type is xs:double:
    • If .000001<=value<=1000000, the value is converted to xs:decimal, and the rules for casting from xs:decimal apply.
    • If value=+0, or value=-0, the result is '0'.
    • Otherwise, the result is the canonical lexical representation of the value, as defined in the XML Schema specification.
  • If the source type is xs:yearMonthDuration or xs:dayTimeDuration, the result is the canonical lexical representation of the value.
  • If the source type is xs:date, xs:dateTime, or xs:time, the result is the lexical representation of the value, with no adjustment for the time zone. If the value has no time zone, the result has no time zone. If the time zone is +00:00 or -00:00, the result has the UTC time zone "Z".
xs:boolean xs:untypedAtomic, xs:string, xs:boolean, xs:double, xs:decimal, xs:integer
  • If the source type is numeric, a value of 0 or NaN is cast to type xs:boolean with a value of false.
  • If the source type is xs:string or xs:untypedAtomic, the value "true" and the value "1" are cast to the xs:boolean value true. The value "false" and the value "0" are cast to the xs:boolean value false. All other values are invalid, and result in an error.
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
  • If the source type is xs:dayTimeDuration, the target value has the same days, hours, minutes and seconds components as the source value. The year component and the month component of the target value are 0.
  • If the source type is xs:yearMonthDuration, the target value has the same years and months components as the source value. The days, hours, minutes and seconds components are 0.
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.