Type Casting in XQuery
By : Kasim Wirama, MCDBA, MCITP
XQuery has several data types. As a result of variety of data types in XQuery, a type of XML operands could be converted from a type to another type. For example an integer value in XML could be converted to double data type. There are 2 data type conversions: implicit conversion and explicit conversion. Let’s see each of them.
Implicit conversion happens when 2 operands are in same family. For example 2 operands in integer and decimal, the integer will be implicitly converted to decimal to yields decimal results. Here is the example:
xs:integer(100) + xs:decimal(101.11)
of course you put the above expression in XML’s query method wrapped with single quotes.
Explicit conversion will be achieved with CAST AS, for example :
xs:integer(100) CAST AS xs:double. If you permits empty sequence may happen, you can put ? after CAST AS <destination data type>. For example :
xs:integer(100) CAST AS xs:double?
That’s topic about casting in XQuery of SQL Server. When it is required to do so, you will know what you need to do to have robust XQuery code.