Back to Library

CAST/CONVERT() in SQL

Converts a value from one data type to another. Both CAST and CONVERT perform the same function with different syntax.

Syntax

-- CAST syntax
CAST(expression AS datatype)

-- CONVERT syntax (SQL Server)
CONVERT(datatype, expression [, style])
Return type
Specified target data type

CAST/CONVERT() Function Example

-- Using CAST
SELECT CAST(numeric_column AS VARCHAR) AS string_value
-- Using CONVERT
SELECT CONVERT(VARCHAR, numeric_column) AS string_value
FROM table;
-- Output: '1000' (number converted to string)

What is CAST/CONVERT() in SQL?

The CAST() and CONVERT() functions in SQL are used to convert a value from one data type to another. These functions are essential for data type compatibility, formatting values, and performing calculations that require specific data types.

  • CAST() follows ANSI SQL standards and is supported across SQL Server, MySQL, PostgreSQL, and Oracle.

  • CONVERT() is SQL Server-specific and provides additional formatting options for date and numeric conversions.

The CAST() and CONVERT() functions are widely used in data transformation, type conversion, and query optimization.

Parameters:

  • expression: Value to convert

  • datatype: Target data type

  • style (CONVERT only): Optional format style code

Example Use Cases:

-- Date formatting

SELECT CAST(order_date AS DATE) AS order_day FROM orders;
-- Output:
order_day
----------
2024-01-15
2024-01-16
2024-01-17
2024-01-18
2024-01-19
2024-01-20

-- Numeric precision

SELECT CAST(price AS DECIMAL(10,2)) AS formatted_price FROM products;
-- Output:
formatted_price
---------------
1299.99
899.50
499.99
1599.99
799.00

-- String to number conversion

SELECT CAST('123' AS INTEGER) AS numeric_value FROM dual;
-- Output:
numeric_value
-------------
123

Notes:

  • Behavior: Returns error or NULL on invalid conversions

  • Performance Considerations: Implicit conversions may impact performance

  • Version Info: Core SQL feature with syntax variations

  • Deprecated/Recommended Alternatives: Some DBMS prefer specific functions

Error Handling:

  • Error: Invalid conversion attempts

  • Recommendation: Validate data before conversion


Supported Databases:

DBMS
Function / Syntax
Example
Behavior with NULL
MySQL

CAST()

CAST(col AS CHAR)

Returns NULL

PostgreSQL

CAST() or ::

CAST(col AS VARCHAR) or col::VARCHAR

Returns NULL

SQL Server

CAST() or CONVERT()

CAST(col AS VARCHAR) or CONVERT(VARCHAR, col)

Returns NULL

SQLite

CAST()

CAST(col AS TEXT)

Returns NULL

BigQuery

CAST() or SAFE_CAST()

CAST(col AS STRING)

Returns NULL

Snowflake

CAST() or ::

CAST(col AS VARCHAR) or col::VARCHAR

Returns NULL

Athena

CAST()

CAST(col AS VARCHAR)

Returns NULL

Related Functions:

CAST/CONVERT()

Converts a value from one data type to another. Both CAST and CONVERT perform the same function with different syntax.

Converts a value from one data type to another. Both CAST and CONVERT perform the same function with different syntax.

Converts a value from one data type to another. Both CAST and CONVERT perform the same function with different syntax.

Converts a value from one data type to another. Both CAST and CONVERT perform the same function with different syntax.

COALESCE()

Returns the first non-null value in a list of expressions, evaluated from left to right.

Returns the first non-null value in a list of expressions, evaluated from left to right.

Returns the first non-null value in a list of expressions, evaluated from left to right.

Returns the first non-null value in a list of expressions, evaluated from left to right.

LAST_VALUE()

Returns the last value in an ordered partition of a result set.

Returns the last value in an ordered partition of a result set.

Returns the last value in an ordered partition of a result set.

Returns the last value in an ordered partition of a result set.

FIRST_VALUE()

Returns the first value in an ordered partition of a result set.

Returns the first value in an ordered partition of a result set.

Returns the first value in an ordered partition of a result set.

Returns the first value in an ordered partition of a result set.

LEAD()

Accesses data from a subsequent row in the result set within a specified window partition.

Accesses data from a subsequent row in the result set within a specified window partition.

Accesses data from a subsequent row in the result set within a specified window partition.

Accesses data from a subsequent row in the result set within a specified window partition.

LAG()

Accesses data from a previous row in the result set within a specified window partition.

Accesses data from a previous row in the result set within a specified window partition.

Accesses data from a previous row in the result set within a specified window partition.

Accesses data from a previous row in the result set within a specified window partition.

DENSE_RANK()

Assigns a rank to rows within a result set partition, with no gaps in rank numbers when ties occur.

Assigns a rank to rows within a result set partition, with no gaps in rank numbers when ties occur.

Assigns a rank to rows within a result set partition, with no gaps in rank numbers when ties occur.

Assigns a rank to rows within a result set partition, with no gaps in rank numbers when ties occur.

CASE

Provides conditional logic in SQL queries, evaluating conditions and returning specified results based on matching conditions.

Provides conditional logic in SQL queries, evaluating conditions and returning specified results based on matching conditions.

Provides conditional logic in SQL queries, evaluating conditions and returning specified results based on matching conditions.

Provides conditional logic in SQL queries, evaluating conditions and returning specified results based on matching conditions.

RANK()

Assigns a rank to rows within a result set partition, with gaps in rank numbers when ties occur.

Assigns a rank to rows within a result set partition, with gaps in rank numbers when ties occur.

Assigns a rank to rows within a result set partition, with gaps in rank numbers when ties occur.

Assigns a rank to rows within a result set partition, with gaps in rank numbers when ties occur.

ROW_NUMBER()

Assigns a unique sequential integer to rows within a result set partition.

Assigns a unique sequential integer to rows within a result set partition.

Assigns a unique sequential integer to rows within a result set partition.

Assigns a unique sequential integer to rows within a result set partition.

One place for all your queries,
directly on your SQL editor