The CAST function is used in SQL to convert a value from one data type to another.
It is useful when you need to change how data is interpreted or formatted.
SELECT CAST(expression AS target_data_type)
FROM table_name;expressionis the value you want to convert.target_data_typeis the data type you want to convert to.
SELECT CAST(salary AS CHAR(10)) AS salary_text
FROM employees;- This query converts the
salarynumber into text format.
- Common conversions include
INT,CHAR,DATE,DECIMAL, andFLOAT. - If conversion fails (invalid format), an error may occur.
- Some databases support both
CAST()andCONVERT()with slight differences.
SELECT CAST(order_date AS DATE) AS simple_date
FROM orders;- This query casts a datetime value into a plain date format.
Describe the problem, challenge, or topic discussed in a video related to SELECT FROM.
What concept was explained or what exercise was solved?
-- Write your SQL code attempt or solution related to SQL COMMAND
SQL COMMANDSQL COMMANDExplanation - Explain what you learned, any key takeaways, or how you solved the problem related to COMMAND._