Skip to content

Latest commit

 

History

History
81 lines (51 loc) · 1.58 KB

File metadata and controls

81 lines (51 loc) · 1.58 KB

📚 CAST

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.


🛠️ Basic Syntax

SELECT CAST(expression AS target_data_type)
FROM table_name;
  • expression is the value you want to convert.
  • target_data_type is the data type you want to convert to.

Example

SELECT CAST(salary AS CHAR(10)) AS salary_text
FROM employees;
  • This query converts the salary number into text format.

Key Points

  • Common conversions include INT, CHAR, DATE, DECIMAL, and FLOAT.
  • If conversion fails (invalid format), an error may occur.
  • Some databases support both CAST() and CONVERT() with slight differences.

Additional Example

SELECT CAST(order_date AS DATE) AS simple_date
FROM orders;
  • This query casts a datetime value into a plain date format.

🎥 Video Notes


📝 Problem Description

Describe the problem, challenge, or topic discussed in a video related to SELECT FROM.
What concept was explained or what exercise was solved?


DataBase Given


💻 My SQL Code

-- Write your SQL code attempt or solution related to SQL COMMAND
SQL COMMAND

🧠 Solution Code / Explanation

SQL COMMAND

Explanation - Explain what you learned, any key takeaways, or how you solved the problem related to COMMAND._


⬅️ Previous: CONCAT Next ➡️ LENGTH