This lesson on CONVERTING DATA TYPES is hands-on and example-driven. You will learn how to use the CAST function to change data from one type to another, such as converting a decimal number into an integer or a text string. You must be aware that converting types can sometimes result in a loss of detail, requiring careful handling of precision and rounding.
What You'll Be Able To Do
- Apply the CAST function to change a numeric value to an integer.
- Specify the precision when converting a number to a decimal type.
- Convert a numeric data type into a variable character string.
- Identify scenarios where data granularity is lost during type conversion.
- Write SQL statements that utilize CAST for required data transformations.
Topics Covered in CONVERTING DATA TYPES
- Need for Conversion (0:00 - 0:05) — Data conversion is necessary when writing SQL statements, such as changing a decimal to an integer.
- Introducing CAST Function (0:05 - 0:15) — The CAST function is used to convert data from its existing type to a new specified type.
- CAST to INTEGER (0:15 - 0:30) — Converting a number like 2.7949 to an integer results in the loss of decimal places and rounding up to 3.
- CAST to DECIMAL (0:30 - 0:45) — You can convert a number to a decimal while specifying the exact number of allowed decimal places.
- CAST to VARCHAR (0:45 - 0:55) — Data can also be converted into a text string using AS VARIABLE CHARACTERS or AS VARCHAR.
- Conversion Caveats (0:55 - 1:10) — Be aware that converting data types, especially to integers, can cause a loss of detail or granularity.
SQL Cheat Sheet
-
CAST(<expression> AS <type>)— Converts data from its current type to the specified new typeSELECT CAST(2.7949 AS INTEGER); -
AS INTEGER— Converts a number to a whole number, losing decimal precisionSELECT CAST(2.7949 AS INTEGER); -- Result: 3 -
AS DECIMAL(P, S)— Converts a number, specifying total digits (P) and scale (S)SELECT CAST(2.7949 AS DECIMAL(3, 2)); -- Result: 2.79 -
AS VARIABLE CHARACTERS— Converts a numeric value into a text string data typeSELECT CAST(2.7949 AS VARCHAR);
Comparison Table
| Resulting Data Type | Conversion Goal | Output for 2.7949 |
|---|---|---|
| INTEGER | Whole number counting | 3 (Rounded up) |
| DECIMAL(X, 2) | Fixed precision decimal | 2.79 (Shortened) |
| VARIABLE CHARACTERS | Text string representation | '2.7949' (Text) |
Common Pitfalls
- Mistake: Assuming decimal values are truncated when converted to integers. Avoid: Remember that CAST often rounds the value to the nearest whole number.
- Mistake: Converting to a decimal type without specifying required precision. Avoid: Always define the precision and scale (P, S) to control the output format.
- Mistake: Expecting the original data type to remain after conversion. Avoid: The new data type dictates how the value is stored and displayed.
FAQs
- Why do I need to convert data types? You need conversions when data types are incompatible for an operation, or when you require a specific format, like rounding to a whole number.
- Does CAST change the data stored in the database? No, CAST only changes the data type for the duration of the query execution. The underlying stored data remains unchanged.
- What happens when I convert a decimal to an integer? The decimal portion is lost, and the number is typically rounded up or down to the nearest whole integer.