What is the CONVERT function in SQL?
CONVERT is one of the most commonly used functions in SQL. The CONVERT() function converts a value (of any type) into a specified datatype.
Syntax
CONVERT(data_type(length), expression, style)
data_type: This is a required parameter. The datatype to convert an expression can be one of the following, bigint, int, smallint, tinyint, bit, decimal, numeric, money, smallmoney, float, real, datetime, smalldatetime, char, varchar, text, nchar, nvarchar, ntext, binary, varbinary, or image.
length: This is an optional parameter. It refers to the length of the resulting data type.
expression: This is the required parameter. The value used to convert to another data type.
style: This is an optional parameter. It refers to the format used to convert within data types, such as a date or string format.
Example
- The data is converted to a
varcharformat:
SELECT CONVERT(varchar, '2021-01-25', 101);
// Run SQL
2021-01-25
- The decimal value of
33.44is converted to an int:
SELECT CONVERT(int, 33.44);
// Run SQL
33
Free Resources