CAST

CAST in SQL is a function used for converting one data type to another. Multiple data types such as INTEGER, VARCHAR, DATE, and others are supported for conversion. Typed values are the result of this conversion process. Note that not all conversions are valid and incorrect conversions can result in errors.

CAST(expression AS type)

  • expression: The value that is intended to be converted into a different data type. This can be a table column, a variable, a constant, or a function that the SQL engine interprets as a value.
  • type: The data type that the expression is to be converted into. It could be a number of different types including INTEGER, VARCHAR, DATE etc. depending on the needs of the query.

Example

SELECT CAST(123 AS CHAR);

Output

'123'

Explanation

The CAST function is used to convert a value from one data type to another. In the given example, the integer value 123 is converted to a string value '123'.

CAST( expression AS type )

  • expression: This refers to the value or data that will undergo type conversion. In SQL, expressions can take various forms like values, functions, subqueries, or any combination of these joined together.
  • type: This parameter represents the target data type that the original expression value is intended to be converted to. PostgreSQL supports various data types ranging from simple ones like INTEGER, TEXT, BOOLEAN to more complex like ARRAY, JSON, etc.

Example

SELECT CAST(10 AS FLOAT);

Output

10.0

Explanation

In the given example, the CAST function converts the integer 10 into a float.

CAST( expression AS data_type [ ( length ) ] )

  • expression: The value to be converted into a different data type.
  • data_type: The target data type to which the expression will be converted.
  • length: This optional parameter specifies the length of the target data type. It is applicable for data types that allow defining a length, such as VARCHAR.

Example

SELECT CAST('1234' AS INT) AS CastOutput;

Output

CastOutput
-----------
1234

Explanation

The CAST function provides a means of explicitly and forcibly converting expression data types. In this case, it converts a string 1234 into an integer. The AS INT denotes the data type that the string is to be converted to, which is integer. The AS keyword is followed by CastOutput, which is an alias used for the resulting converted output.

CAST(value AS datatype);

  • value: The specific data, or column’s data, that you want to convert.
  • datatype: The target data type to which the value is to be converted.

Example

SELECT CAST('500' AS NUMBER) FROM dual;

Output

500

Explanation

In the above example, the CAST function is used to convert the string '500' to a number. The FROM dual part is specific to Oracle SQL, which is a dummy table used for calculations.

CAST(expression AS type)

  • expression: This is the value that will be converted, such as a column in a table.
  • type: This denotes the target data type that the “expression” will be turned into. The allowed types in SQLite are

Example

SELECT CAST('200' AS INTEGER);

Output

200

Explanation

The CAST function in the example is used to convert the string ‘200’ into an integer 200. This operation is known as type casting.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.