cast()

Scalar. Converts the value of one datatype to another datatype allowing overflows and truncation.

Syntax

cast ( type, number )

Parameters

type

Any datatype, except binary or string.

number

A datatype that can be cast to the new specified datatype.

Usage

The type argument must be a numeric type, money type, or a date/time type. You can cast expressions of any type except binary or string types.

Casting from larger types to smaller types may cause overflow. Casting from decimal types (like float or money) to nondecimal types (like integer) truncates the decimal portion. Both overflows and truncation are allowed. Use this function to force a cast in places where an implicit cast is disallowed, such as when converting an integer to a long.

When comparing values of varying scale, cast one value to the other to make the two values compatible. For example, you can compare money values of different scale only by casting to a common type.

How to cast money values of different scale depends on how you compare the two values:
  • If you set 100.55D2, a money(2) type, as greater than (>) 100.545D3, a money(3) type, the result is false because the values are represented internally without the decimal point. Therefore, 10055 cannot be greater than 100545. In this example, you can perform casting on either value to produce a true result. When you cast 10055 to 100545, the comparison becomes 100550>100545, which is true. When you cast 100545 to 10055, the comparison becomes 10055>10054, which is also true.
  • If you set 100.55D2 as equal (=) to 100.556D3, the result is false. In this example, the result changes depending on which value you cast. When you cast 10055 to 100556, the comparison becomes 100550=100556, which is false. When you cast 100556 to 10055, the comparison becomes 10055=10055, which is true.

You may prefer to cast lower scale values to higher scale values to avoid incorrect comparison results and to maintain scale.

Example

cast ( integer, 1.23) returns 1.