Beginner

282. CAST Function

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

📘 Lesson 282 of 728 ⏱ 10 min 🎯 Intermediate

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

Data stored in databases may not always have the required type for a calculation, comparison, or report. CAST allows analysts to convert values into an appropriate type.

Convert a text value containing a number into an integer.

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

CAST is widely used in data cleaning, calculations, joins, reporting, and preparing data for analysis.

  • CAST converts data types.
  • It uses explicit conversion syntax.
  • It is useful in calculations.
  • It can help resolve type mismatches.
  • Invalid conversions may produce errors.
What does CAST do?

CAST explicitly converts an expression from one data type to another.

Why is CAST important in SQL?

CAST helps ensure values have the correct data type for calculations, comparisons, joins, and reporting.

Have you finished this lesson?

Introduction to SQL – All Lessons