The CAST function explicitly converts a value from one data type to another supported data type.
Data stored in databases may not always have the data type required for calculations, comparisons, or reporting.
Convert a text value containing a number into an integer.
SELECT
CAST(SalaryText AS DECIMAL(12,2)) AS Salary
FROM Employees;
CAST is used in data transformation, reporting, calculations, filtering, joins, and data preparation.
- CAST converts data types.
- It uses explicit conversion syntax.
- The target data type must be compatible.
- It is useful in calculations.
- Conversion errors can occur when values are invalid.
What is CAST used for?
CAST explicitly converts a value from one data type to another.
Why might CAST be needed in SQL?
CAST may be required when data is stored in an unsuitable type for calculations, comparisons, or reporting.