Sdílet prostřednictvím


CAST

Converts an expression of one data type to another.

CAST ( expression AS data_type )

Arguments

  • expression
    Any valid expression that is convertible to data_type.
  • data_type
    The target system-supplied data type. It must be a primitive (scalar) type. The data_type used depends on the query space. If a query is executed with the EntityCommand, the data type is a type defined in the conceptual model. For more information, see CSDL Specification. If a query is executed with ObjectQuery, the data type is a common language runtime (CLR) type.

Return Value

Returns the same value as data_type.

Remarks

The cast expression has similar semantics to the Transact-SQL CONVERT expression. The cast expression is used to convert a value of one type into a value of another type.

CAST( e as T )

If e is of some type S, and S is convertible to T, then the above expression is a valid cast expression. T must be a primitive (scalar) type.

Values for the precision and scale facets may optionally be provided when casting to Edm.Decimal. If not explicitly provided, the default values for precision and scale are 18 and 0, respectively. Specifically, the following overloads are supported for Decimal:

  • CAST( d as Edm.Decimal );

  • CAST( d as Edm.Decimal(precision) );

  • CAST( d as Edm.Decimal(precision, scale) );

The use of a cast expression is considered an explicit conversion. Explicit conversions might truncate data or lose precision.

Note

CAST is only supported over primitive types and enumeration member types.

Example

The following Entity SQL query uses the CAST operator to cast an expression of one data type to another. The query is based on the AdventureWorks Sales Model. To compile and run this query, follow these steps:

  1. Follow the procedure in How to: Execute a Query that Returns PrimitiveType Results.

  2. Pass the following query as an argument to the ExecutePrimitiveTypeQuery method:

SELECT VALUE cast(p.ListPrice as Edm.Int32) 
    FROM AdventureWorksEntities.Products as p order by p.ListPrice

See Also

Concepts

Entity SQL Reference