📅  最后修改于: 2020-11-19 02:09:19             🧑  作者: Mango
MySQL中的CONVERT()函数用于将值从一种数据类型转换为表达式中指定的另一种数据类型。 MySQL还允许它将指定的值从一个字符集转换为另一个字符集。
以下是此功能可以完美函数的数据类型:
Datatype | Descriptions |
---|---|
DATE | It converts the value into DATE datatype that responsible for the date portion only. It always results in the “YYYY-MM-DD” format. It supports the range of DATE in ‘1000-01-01’ to ‘9999-12-31’. |
DATETIME | It converts the value into the DATETIME data type that responsible for the date and time portion both. It always results in the “YYYY-MM-DD HH:MM:SS” format. It support the range in ‘1000-01-01 00:00:00’ to ‘9999-12-31 23:59:59’. |
TIME | It converts the value into a TIME data type that responsible for the time portion only. It always results in the “HH:MM:SS” format. It supports the range of time in ‘-838:59:59’ to ‘838:59:59’. |
CHAR | It converts a value to the CHAR data type, which has a fixed-length string. |
SIGNED | It converts a value to SIGNED datatype, which has signed 64-bit integer. |
UNSIGNED | It converts a value to the UNSIGNED datatype, which has unsigned 64-bit integer. |
DECIMAL | It converts a value to the DECIMAL data type, which has a decimal string. |
BINARY | It converts a value to the BINARY data type, which has a binary string. |
以下是MySQL中CONVERT()函数的语法:
CONVERT(expression, datatype);
OR,
CONVERT(expression USING character_set);
该语法接受以下参数,下面将对其进行讨论:
Parameter | Requirement | Descriptions |
---|---|---|
expression | Required | It is a specified value going to be converted into another specific datatype. |
datatype | Required | It specifies the desired data type in which we want to be converted. |
character_set | Required | It specifies the desired character set in which we want to be converted. |
它将返回一个值,在该值中我们要转换数据类型或字符集。
该函数可以支持以下版本:
让我们通过以下示例了解MySQL CONVERT()函数。我们可以直接在SELECT语句中使用CONVERT函数。
当我们实现该语句时,它将把值转换为DATETIME数据类型。
SELECT CONVERT("2018-11-30", DATETIME);
输出量
当我们执行该语句时,它将把值转换为UNSIGNED数据类型。
SELECT CONVERT(4-7, UNSIGNED);
输出量
当我们实现该语句时,它将把值转换为SIGNED数据类型。
SELECT CONVERT(CONVERT(4-7, UNSIGNED), SIGNED);
输出量
当我们实现该语句时,它将把字符串值转换为utf8mb4字符集。
SELECT CONVERT('javatpoint' USING utf8mb4);
输出量
有时需要在不同字符集之间转换字符串。在这种情况下,我们使用以下语句进行转换:
SELECT CONVERT('javatpoint', CHAR CHARACTER SET utf8mb4);
输出量
以下语句首先将整数值转换为字符串数据类型,然后与另一个指定的字符串执行串联。
SELECT CONCAT('CONVERT Function Example ## ',CONVERT(5, CHAR));
输出量
在此示例中,我们将看到CONVERT函数如何与表一起使用。让我们首先创建一个包含以下数据的表“ Orders”:
在上表中,Order_Date为DATE数据类型。因此,如果要在选定的时间范围内获得产品名称,请执行以下语句。
SELECT Order_ID, Product_Name FROM orders
WHERE Order_Date
BETWEEN CONVERT('2020-02-01', DATETIME) AND CONVERT('2020-03-10', DATETIME);
我们将得到以下输出:
下表总结了CAST函数和CONVERT函数之间的主要区别:
SN | CAST() Function | CONVERT() Function |
---|---|---|
1. | We use it to convert one data type into the other data type. | We use it to convert one data type into the other data type. |
2. | It cannot allow us to convert a character set. | It can be used to convert the character set. |
3. | It is the part of ANSI-SQL specification. | It is not the part of ANSI-SQL specification. |
4. | It uses “AS” for separating the parameter. | It uses “comma(,)” or “USING” for separating the parameter. |