0
0
SQLquery~10 mins

CAST and CONVERT for type changes in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - CAST and CONVERT for type changes
Start with original value
Choose target data type
Apply CAST or CONVERT
Check if conversion is valid
Return converted value
End
The flow shows how a value is taken, a target type is chosen, then CAST or CONVERT changes the type, checking validity before returning the result or error.
Execution Sample
SQL
SELECT CAST('123' AS INT) AS cast_result,
       CONVERT(VARCHAR, 456) AS convert_result;
This query converts a string '123' to an integer using CAST and converts an integer 456 to a string using CONVERT.
Execution Table
StepExpressionOperationResultNotes
1CAST('123' AS INT)Convert string '123' to INT123String '123' is numeric, conversion succeeds
2CONVERT(VARCHAR, 456)Convert INT 456 to string'456'Integer 456 converted to string '456'
3CAST('abc' AS INT)Attempt to convert non-numeric stringError or NULLConversion fails, returns error or NULL depending on SQL dialect
4CONVERT(DATE, '2024-06-01')Convert string to DATE2024-06-01Valid date string converted to DATE type
5CAST(123.45 AS INT)Convert float to INT123Decimal truncated to integer part
6EndNo more expressionsQuery returns two columns with resultsExecution complete
💡 All expressions processed; conversion errors handled by returning NULL or error depending on SQL dialect
Variable Tracker
ExpressionInitial ValueAfter Conversion
CAST('123' AS INT)'123' (string)123 (int)
CONVERT(VARCHAR, 456)456 (int)'456' (string)
CAST('abc' AS INT)'abc' (string)Error or NULL
CONVERT(DATE, '2024-06-01')'2024-06-01' (string)2024-06-01 (date)
CAST(123.45 AS INT)123.45 (float)123 (int)
Key Moments - 3 Insights
Why does CAST('abc' AS INT) fail while CAST('123' AS INT) works?
Because 'abc' is not a numeric string, the conversion to INT is invalid and causes an error or NULL, as shown in execution_table step 3.
What happens when converting a float to an INT using CAST?
The decimal part is truncated, not rounded, so 123.45 becomes 123, as shown in execution_table step 5.
How does CONVERT differ from CAST in usage?
CONVERT allows specifying the target type first and can include style parameters for formatting; CAST uses a simpler syntax. Both perform type changes as shown in steps 1 and 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of CAST('123' AS INT) at step 1?
A'123'
B123
CError
DNULL
💡 Hint
Check the 'Result' column in execution_table row for step 1
At which step does the conversion fail due to invalid input?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look for 'Error or NULL' in the 'Result' column in execution_table
If we change CAST(123.45 AS INT) to CAST(123.99 AS INT), what would be the result?
A123
BError
C124
DNULL
💡 Hint
Refer to execution_table step 5 showing truncation, not rounding
Concept Snapshot
CAST and CONVERT change data types in SQL.
CAST(expression AS target_type) is standard syntax.
CONVERT(target_type, expression) is similar, with optional style.
Invalid conversions cause errors or NULL.
Casting float to int truncates decimals.
Useful for data type compatibility and formatting.
Full Transcript
This lesson shows how SQL changes data types using CAST and CONVERT. We start with a value, choose a target type, then apply CAST or CONVERT. If the conversion is valid, the new value is returned; if not, an error or NULL occurs. For example, casting the string '123' to integer gives 123, but casting 'abc' to integer fails. Converting integer 456 to string results in '456'. Casting a float like 123.45 to integer truncates to 123. These functions help manage data types in queries.