Table of Contents

Class Cast

Namespace
YndigoBlue.Velocity.Model
Assembly
YndigoBlue.Velocity.dll

Represents a CAST operation that converts a value or expression to a different data type.

public class Cast : IElement
Inheritance
Cast
Implements

Examples

Cast string to integer:

// CAST(user_input AS INTEGER)
Column userInputColumn = inputTable["user_input"];
Cast castToInt = new Cast(DataType.Integer, userInputColumn);

Expression expr = new Expression("numeric_input", castToInt);
query.AddSelectItem(expr);

// SQL: SELECT CAST(user_input AS INTEGER) AS numeric_input

Cast to decimal with precision:

// CAST(price AS DECIMAL(10, 2))
Column priceColumn = ordersTable["price"];
Cast castToDecimal = new Cast(DataType.Decimal, priceColumn);
castToDecimal.Precision = 10;
castToDecimal.Scale = 2;

Expression expr = new Expression("formatted_price", castToDecimal);
query.AddSelectItem(expr);

// SQL: SELECT CAST(price AS DECIMAL(10, 2)) AS formatted_price

Cast expression result:

// CAST((price * quantity) AS DECIMAL(10, 2))
Expression calculation = new Expression();
calculation.Add(ordersTable["price"]);
calculation.Add(new ArithmeticOperator(ArithmeticType.Multiply));
calculation.Add(ordersTable["quantity"]);

Cast castCalculation = new Cast(DataType.Decimal, calculation.Elements);
castCalculation.Precision = 10;
castCalculation.Scale = 2;

Expression totalExpr = new Expression("total", castCalculation);
query.AddSelectItem(totalExpr);

// SQL: SELECT CAST((price * quantity) AS DECIMAL(10, 2)) AS total

Cast to VARCHAR with size:

// CAST(id AS VARCHAR(50))
Column idColumn = usersTable["id"];
Cast castToString = new Cast(DataType.VarChar, idColumn);
castToString.Size = 50;

Expression expr = new Expression("id_string", castToString);
query.AddSelectItem(expr);

// SQL: SELECT CAST(id AS VARCHAR(50)) AS id_string

Cast string to date:

// CAST(date_string AS DATE)
Column dateStringColumn = eventsTable["date_string"];
Cast castToDate = new Cast(DataType.Date, dateStringColumn);

Expression expr = new Expression("event_date", castToDate);
query.AddSelectItem(expr);

// SQL: SELECT CAST(date_string AS DATE) AS event_date

Remarks

Cast is used to explicitly convert columns, expressions, or literal values to a different data type in SQL queries. This is commonly needed when:

  • Performing operations on columns with incompatible types
  • Ensuring numeric precision in calculations
  • Converting strings to dates or numbers
  • Formatting output in a specific data type

The target data type can have additional parameters:

  • Size - For character and binary types (e.g., VARCHAR(100))
  • Precision - For numeric types (total number of digits)
  • Scale - For decimal types (digits after decimal point)

Constructors

Cast(DataType, IEnumerable<IElement>)

Creates a new cast operation for multiple elements.

Cast(DataType, IElement)

Creates a new cast operation for a single element.

Cast(DataType, IElement[])

Creates a new cast operation for multiple elements.

Properties

DataType

Gets the target data type for the cast operation.

Elements

Gets or sets the elements (columns, expressions, etc.) to be cast.

Precision

Gets or sets the precision for numeric data types (total number of digits).

Scale

Gets or sets the scale for decimal data types (digits after the decimal point).

Size

Gets or sets the size for character and binary data types.