Type Conversion Functions

Functions for converting an expression of one data type to another.

CAST

Synopsis:

  1. CAST(
  2. expression
  3. AS data_type)

Expression to cast

Target data type to cast to

Description: Casts the result of the given expression to the target data type. If the cast is not possible (for example because of target type is too narrow or because the value itself cannot be converted), the query fails.

  1. SELECT CAST('123' AS INT) AS int;
  2. int
  3. ---------------
  4. 123
  1. SELECT CAST(123 AS VARCHAR) AS string;
  2. string
  3. ---------------
  4. 123
  1. SELECT YEAR(CAST('2018-05-19T11:23:45Z' AS TIMESTAMP)) AS year;
  2. year
  3. ---------------
  4. 2018

Both ANSI SQL and Elasticsearch SQL types are supported with the former taking precedence. This only affects FLOAT which due naming conflict, is interpreted as ANSI SQL and thus maps to double in Elasticsearch as oppose to float. To obtain an Elasticsearch float, perform casting to its SQL equivalent, real type.

CONVERT

Synopsis:

  1. CONVERT(
  2. expression,
  3. data_type)

Expression to convert

Target data type to convert to

Description: Works exactly like CAST with slightly different syntax. Moreover, apart from the standard data types it supports the corresponding ODBC data types.

  1. SELECT CONVERT('123', SQL_INTEGER) AS int;
  2. int
  3. ---------------
  4. 123
  1. SELECT CONVERT('123', INTEGER) AS int;
  2. int
  3. ---------------
  4. 123