This tutorial explains how to use the MySQL CONVERT function with syntax and examples.
The MySQL CONVERT function converts a value from one datatype to another, or one character set to another.
There are 2 syntaxes for the CONVERT function - one syntax to convert datatypes and one syntax to convert character sets.
The first syntax for the CONVERT function is used to convert one datatype to another datatype in MySQL:
OR
The second syntax for the CONVERT function is used to convert one character set to another character set:
The value to convert.
The datatype that you wish to convert value to. type can be one of the following:
Value | Description |
---|---|
DATE | Converts value to DATE type, which has a date portion only. Format is 'YYYY-MM-DD'. Supported range is '1000-01-01' to '9999-12-31'. |
DATETIME | Converts value to DATETIME type, which has both date and time portions. Format is 'YYYY-MM-DD HH:MM:SS'. Supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59'. |
TIME | Converts value to TIME type, which has a time portion only. Format is 'HH:MM:SS'. Supported range is '-838:59:59' to '838:59:59'. |
CHAR | Converts value to CHAR type, which is a fixed length string. |
SIGNED | Converts value to SIGNED type, which is a signed 64-bit integer. |
UNSIGNED | Converts value to UNSIGNED type, which is an unsigned 64-bit integer. |
BINARY | Converts value to BINARY type, which is a binary string. |
The character set to convert to.
The CONVERT function can be used in the following versions of MySQL:
Let's look at some MySQL CONVERT function examples and explore how to use the CONVERT function in MySQL.
The first syntax for the MySQL CONVERT function allows you to convert a value from one datatype to another datatype. Let's look at how to use the CONVERT function to convert a value to a DATE type. For example:
This CONVERT example takes the value '2014-02-28' and converts it to a DATE datatype.
This CONVERT function example shows how to convert a value to a DATETIME type. For example:
This CONVERT example takes the value '2014-02-28 08:14:57' and converts it to a DATETIME datatype.
This CONVERT function example shows how to convert a value to a TIME type. For example:
This CONVERT example takes the value '08:14:57' and converts it to a TIME datatype.
This CONVERT function example shows how to convert a value to a CHAR type. For example:
This CONVERT example takes the value 125 and converts it as a CHAR datatype with the value of '125'.
This CONVERT function example shows how to convert a value to a SIGNED type. For example:
This CONVERT example takes the value 4-5 and converts it as a SIGNED datatype with the value of -2.
This CONVERT function example shows how to convert a value to an UNSIGNED type. For example:
This CONVERT example takes the value 4-5 and converts it as an UNSIGNED datatype with the value of 18446744073709551614.
This CONVERT function example shows how to convert a value to a BINARY type. For example:
This CONVERT example takes the value '4' and converts it as a BINARY datatype with the binary string value of '4'.
The second syntax for the MySQL CONVERT function allows you to convert a value from one character set to another. Let's look at how to use the CONVERT function to convert between character sets. For example:
This CONVERT example takes the value 'AODBA.com' and converts it from the current character set to the utf8 character set.
We could change our example above to convert the value 'AODBA.com' to the latin1 character set as follows: