0
0
SQLquery~5 mins

CAST and CONVERT for type changes in SQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the CAST function do in SQL?
CAST changes a value from one data type to another in SQL. For example, it can turn a number into text or a date into a string.
Click to reveal answer
intermediate
How is CONVERT different from CAST in SQL?
CONVERT also changes data types but often allows formatting options, especially for dates. CAST is more standard and simpler.
Click to reveal answer
beginner
Write a SQL example using CAST to convert an integer 123 to a string.
SELECT CAST(123 AS VARCHAR(10));
Click to reveal answer
intermediate
Write a SQL example using CONVERT to change a date to a string with format style 101 (mm/dd/yyyy).
SELECT CONVERT(VARCHAR(10), GETDATE(), 101);
Click to reveal answer
beginner
Why might you use CAST or CONVERT in a SQL query?
To make sure data types match when comparing or combining data, or to format data for display or calculations.
Click to reveal answer
Which SQL function is more standard across different database systems for type conversion?
ACONVERT
BFORMAT
CCAST
DTO_CHAR
What does this SQL do? SELECT CAST('2024-06-01' AS DATE);
AConverts a string to a date
BConverts a date to a string
CChanges a number to a string
DChanges a string to a number
Which CONVERT style code formats a date as mm/dd/yyyy in SQL Server?
A120
B101
C103
D1
If you want to convert an integer to a string in SQL, which is correct?
ACAST(123 AS VARCHAR)
BCONVERT(INT, '123')
CCAST('123' AS INT)
DCONVERT(DATE, 123)
Which function allows you to specify a style or format when converting types?
ASUBSTRING
BCAST
CLEN
DCONVERT
Explain how CAST and CONVERT are used to change data types in SQL and when you might choose one over the other.
Think about standard use versus formatting needs.
You got /4 concepts.
    Describe a real-life example where converting data types in SQL is necessary.
    Imagine working with a report or data from different sources.
    You got /4 concepts.