Beginner

331. CAST Function

Learn how to convert a value from one data type to another.

📘 Lesson 331 of 728 ⏱ 10 min 🎯 Beginner

The CAST function converts an expression from one data type to another supported data type.

Data often arrives in the wrong format. CAST allows analysts to convert values so they can be compared, calculated, filtered, or displayed correctly.

Convert a text value containing a number into an integer.

SQL
SELECT
    CAST(SalaryText AS DECIMAL(12,2)) AS Salary
FROM Employees;

CAST is commonly used in data cleaning, calculations, reporting, joins, and ETL processes.

  • CAST converts data types.
  • It is useful for numeric calculations.
  • It can convert text to dates or numbers.
  • It is widely used in data preparation.
  • Conversion must be compatible with the target type.
What does CAST do?

CAST converts an expression from one data type to another.

Why is CAST important in SQL?

CAST allows values to be converted into suitable data types for calculations, comparisons, filtering, and reporting.

Have you finished this lesson?

Introduction to SQL – All Lessons