In SQL, CONVERT function is used to convert a value from one data type to another data type.
CONVERT(data_type, expression)
• Specify the 'expression/value' that you want to convert from one data type to another.
• Specify the desired 'data type' to which you want to convert the value or expression.
1. Let's assume we have a table named "[Employees]".
2. Let's assume you want to convert the Joining_Date values from datetime format to date format.
3. Run below SQL statement :
SELECT *, CONVERT(Date, Joining_Date) AS [Date] FROM [Employees];
4. In above statement, CAST function converts the Joining_Date values from datetime format to date format.
Using this function in a select statement won't modify the [Employees] table directly, but it will only be reflected in the select statement's output.
CONVERT() in SQL converts a value from one data type to another.
CAST() and CONVERT() functions in SQL Server perform similar functions of converting data types, with slight differences in syntax and behavior.
To convert a SQL query to another SQL query, you typically rewrite or modify the original query to achieve the desired output or functionality.
The function to convert to a string in SQL varies based on the database system; for example, in SQL Server, you can use CONVERT() or CAST(), while in MySQL, you might use CONVERT() or CAST() as well.