Challenge - 5 Problems
CAST and CONVERT Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What is the output of this CAST query?
Consider a table Products with a column
Price of type VARCHAR storing numeric values as strings. What will be the result of this query?SELECT CAST(Price AS INT) AS PriceInt FROM Products WHERE Price = '100';
SQL
SELECT CAST(Price AS INT) AS PriceInt FROM Products WHERE Price = '100';
Attempts:
2 left
💡 Hint
CAST converts string numbers to integers for numeric operations.
✗ Incorrect
CAST changes the string '100' to the integer 100, so the output shows PriceInt as 100.
📝 Syntax
intermediate2:00remaining
Which option correctly converts a datetime to a string in SQL?
You want to convert a datetime column
OrderDate to a string format using CONVERT. Which of these options is syntactically correct?Attempts:
2 left
💡 Hint
The correct syntax is CONVERT(target_type(length), source, style).
✗ Incorrect
Option A uses the correct syntax: CONVERT(VARCHAR(10), OrderDate, 101).
❓ optimization
advanced2:00remaining
Which query is more efficient for converting and filtering numeric strings?
Given a table
Sales with a VARCHAR column Amount storing numbers as strings, you want to select rows where Amount is greater than 100 after conversion. Which query is more efficient?Attempts:
2 left
💡 Hint
Filtering on the original string column can use indexes better than converting each row.
✗ Incorrect
Option C compares strings directly, which can use indexes and is faster than converting each row.
🔧 Debug
advanced2:00remaining
Why does this CAST query raise an error?
You run this query:
Why does it fail?
SELECT CAST('abc' AS INT) AS NumberValue;Why does it fail?
SQL
SELECT CAST('abc' AS INT) AS NumberValue;
Attempts:
2 left
💡 Hint
CAST fails when the string cannot be interpreted as a number.
✗ Incorrect
The string 'abc' is not numeric, so CAST to INT raises a conversion error.
🧠 Conceptual
expert2:00remaining
What is the difference between CAST and CONVERT in SQL?
Which statement best describes the difference between CAST and CONVERT for type changes in SQL?
Attempts:
2 left
💡 Hint
Think about standards and extra features like formatting styles.
✗ Incorrect
CAST is part of the ANSI SQL standard and works across many databases; CONVERT is specific (e.g., SQL Server) and allows style codes for formatting.