Bird
Raised Fist0
MySQLquery~10 mins

Type casting and conversion in MySQL - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Type casting and conversion
Start with original value
↓
Apply CAST or CONVERT function
↓
Value changes type
↓
Use converted value in query
↓
Return result with new type
This flow shows how a value is taken, converted to a new type using CAST or CONVERT, then used in the query with the new type.
Execution Sample
MySQL
SELECT CAST('123' AS UNSIGNED) AS num;
SELECT CONVERT('2024-06-01', DATE) AS date_val;
These queries convert a string to an unsigned number and a string to a date type.
Execution Table
StepQueryValue BeforeConversion FunctionValue AfterResult Type
1SELECT CAST('123' AS UNSIGNED) AS num;'123'CAST('123' AS UNSIGNED)123UNSIGNED INTEGER
2SELECT CONVERT('2024-06-01', DATE) AS date_val;'2024-06-01'CONVERT('2024-06-01', DATE)2024-06-01DATE
3End of queries----
💡 All conversions complete, queries return values in new types.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
value'123'123123123
value'2024-06-01''2024-06-01''2024-06-01''2024-06-01'
Key Moments - 2 Insights
Why does CAST('123' AS UNSIGNED) return 123 as a number, not a string?
CAST changes the data type from string to number, so the output is numeric, as shown in execution_table row 1.
What happens if the string cannot be converted to the target type?
MySQL returns 0 or NULL depending on context; this is not shown here but is important to remember.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result type after casting '123'?
AUNSIGNED INTEGER
BSTRING
CDATE
DFLOAT
💡 Hint
Check the 'Result Type' column in execution_table row 1.
At which step does the value change from string to date?
AStep 1
BStep 2
CStep 3
DNo change
💡 Hint
Look at the 'Value After' and 'Result Type' columns in execution_table row 2.
If the string 'abc' is cast to UNSIGNED, what would likely happen?
AReturns NULL
BReturns 'abc'
CReturns 0
DReturns error
💡 Hint
Recall key_moments about invalid conversions returning 0 or NULL.
Concept Snapshot
Type casting changes a value's data type.
Use CAST(value AS type) or CONVERT(value, type).
Common types: UNSIGNED, SIGNED, CHAR, DATE.
Invalid casts may return 0 or NULL.
Useful for ensuring correct data types in queries.
Full Transcript
This lesson shows how MySQL converts data types using CAST and CONVERT functions. We start with a string value, apply a conversion function, and get a new value with a different type. For example, casting '123' as UNSIGNED turns it into the number 123. Converting '2024-06-01' to DATE changes the string into a date type. The execution table tracks each step, showing the original value, the conversion applied, and the resulting value and type. Beginners often wonder why the output changes type; it's because CAST explicitly changes the data type. If a string cannot be converted properly, MySQL returns 0 or NULL instead of an error. The quiz questions help reinforce understanding by asking about result types and behavior on invalid input. Remember, type casting is essential to control data formats in your queries.

Practice

(1/5)
1. What does the CAST() function do in MySQL?
easy
A. It changes the database user permissions.
B. It creates a new table in the database.
C. It converts a value from one data type to another.
D. It deletes rows from a table.

Solution

  1. Step 1: Understand the purpose of CAST()

    The CAST() function is used to change the data type of a value, for example from string to integer.
  2. Step 2: Compare with other options

    Options A, B, and C describe unrelated database operations, not type conversion.
  3. Final Answer:

    It converts a value from one data type to another. -> Option C
  4. Quick Check:

    CAST() = type conversion [OK]
Hint: CAST() changes data types, not structure or permissions [OK]
Common Mistakes:
  • Thinking CAST() creates or deletes tables
  • Confusing CAST() with permission commands
  • Assuming CAST() modifies data values without type change
2. Which of the following is the correct syntax to convert a string '123' to an integer in MySQL?
easy
A. CAST('123' AS SIGNED)
B. CONVERT('123', TO INTEGER)
C. CAST('123' TO INTEGER)
D. CONVERT('123' AS INT)

Solution

  1. Step 1: Recall correct CAST syntax

    In MySQL, the correct syntax is CAST(value AS type), so CAST('123' AS SIGNED) is valid.
  2. Step 2: Check other options for syntax errors

    Options A, B, and D use incorrect syntax: A uses 'TO' instead of 'AS', B and D misuse CONVERT syntax, which is invalid in MySQL.
  3. Final Answer:

    CAST('123' AS SIGNED) -> Option A
  4. Quick Check:

    CAST(value AS type) = correct syntax [OK]
Hint: Use CAST(value AS type) format for conversions [OK]
Common Mistakes:
  • Using TO instead of AS in CAST
  • Using CONVERT with wrong syntax
  • Mixing syntax from other SQL dialects
3. What is the result of this query?
SELECT CAST('2023-06-15' AS DATE) + INTERVAL 5 DAY AS new_date;
medium
A. 2023-06-20
B. 2023-06-10
C. 2023-06-15 00:00:00
D. Error: Invalid date operation

Solution

  1. Step 1: Convert string to DATE type

    CAST('2023-06-15' AS DATE) converts the string to a date value representing June 15, 2023.
  2. Step 2: Add 5 days interval

    Adding INTERVAL 5 DAY moves the date forward by 5 days, resulting in June 20, 2023.
  3. Final Answer:

    2023-06-20 -> Option A
  4. Quick Check:

    Date + 5 days = 2023-06-20 [OK]
Hint: Add intervals to dates after casting string to DATE [OK]
Common Mistakes:
  • Forgetting to cast string to DATE before adding interval
  • Adding interval to string without conversion
  • Expecting time part in result
4. Identify the error in this query:
SELECT CONVERT('abc' AS UNSIGNED);
medium
A. Returns NULL
B. No error, returns 0
C. Runtime error: cannot convert 'abc' to number
D. Syntax error: wrong CONVERT syntax

Solution

  1. Step 1: Check CONVERT syntax

    CONVERT(value, type) is correct syntax in MySQL, but 'AS' keyword is invalid here, so syntax error would occur.
  2. Step 2: Analyze actual query

    The query uses CONVERT('abc' AS UNSIGNED), which is invalid syntax and causes syntax error, but if corrected to CONVERT('abc', UNSIGNED), it returns 0 because 'abc' cannot be converted to a number.
  3. Final Answer:

    Syntax error: wrong CONVERT syntax -> Option D
  4. Quick Check:

    CONVERT() syntax must be CONVERT(value, type) [OK]
Hint: Use CONVERT(value, type) without AS keyword [OK]
Common Mistakes:
  • Using AS keyword inside CONVERT()
  • Expecting NULL instead of 0 for invalid numeric conversion
  • Confusing CONVERT syntax with CAST
5. You have a table sales with a column amount stored as VARCHAR. You want to calculate the total sales as a number. Which query correctly converts and sums the amounts?
hard
A. SELECT SUM(amount) FROM sales;
B. SELECT SUM(CAST(amount AS DECIMAL(10,2))) FROM sales;
C. SELECT SUM(CONVERT(amount AS CHAR)) FROM sales;
D. SELECT SUM(CAST(amount AS DATE)) FROM sales;

Solution

  1. Step 1: Understand data type of amount

    amount is VARCHAR, so summing directly treats values as strings, causing wrong results or errors.
  2. Step 2: Convert amount to numeric type before summing

    CAST(amount AS DECIMAL(10,2)) converts string to decimal number, allowing correct sum calculation.
  3. Step 3: Check other options

    SELECT SUM(amount) FROM sales; sums strings (incorrect), C converts to CHAR (string again), D converts to DATE (invalid for amounts).
  4. Final Answer:

    SELECT SUM(CAST(amount AS DECIMAL(10,2))) FROM sales; -> Option B
  5. Quick Check:

    Convert strings to numbers before SUM [OK]
Hint: CAST strings to DECIMAL before SUM to avoid errors [OK]
Common Mistakes:
  • Summing VARCHAR without conversion
  • Casting to wrong type like DATE or CHAR
  • Using CONVERT with wrong syntax
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard