CONVERT
is one of the most commonly used functions in SQL. The CONVERT()
function converts a value (of any type) into a specified datatype.
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.
varchar
format:SELECT CONVERT(varchar, '2021-01-25', 101);
// Run SQL
2021-01-25
33.44
is converted to an int:SELECT CONVERT(int, 33.44);
// Run SQL
33
Free Resources