Using MySQL Conversion Functions
MySQL Conversion Functions convert a value from one data type to another. Conversions can be conducted between
string, date, and numeric type of data.
There are three Conversion Functions in MySQL: CONVERT, CAST, BINARY.
Function |
Description |
CONVERT |
CONVERT can be used in either of the following two forms:
Form 1: CONVERT(expr,type)
In this form, CONVERT takes a value in the form of expr and converts it to a value of type.
The type can be one of the following values:
BINARY[(N)]
Converts a value to BINARY data type.
After a value is converted to BINARY type, comparisons are conducted byte by byte rather than
character by character. This causes the comparison to be case sensitive even if the column isn't
defined as BINARY or BLOB. BINARY also causes trailing spaces to be significant.
See Practice #1-1.
CHAR[(N)]
Converts a value to character type of data.
CHAR(N) causes the convert to use no more than N characters of the argument.
See Practice #1-2.
DATE
Converts a value to DATE data type in the format of YYYY-MM-DD. See Practice #1-3.
DATETIME
Converts a value to DATETIME data type in the format of YYYY-MM-DD HH:MM:SS. See Practice #1-4.
DECIMAL[(M[,D])]
Converts a value to DECIMAL data type. The optional arguments M and D specify the precision (M specifies the total number of digits)
and the scale (D specifies the number of digits after the decimal point) of the
decimal value. The default precision is two digits after the decimal point. See Practice #1-5.
SIGNED [INTEGER]
Converts an unsigned integer to a signed integer.
For an example of a practical use, read Tip #2 in How to enforce data type constraint article.
TIME
Converts a signed integer to an unsigned integer.
See Practice #1-7.
UNSIGNED [INTEGER]
Converts a signed integer to an unsigned integer.
See Practice #1-8.
Form 2: CONVERT(expr USING transcoding_name)
In this form, CONVERT() with USING is used to convert data between different character sets (utf8, latin1, ...)
In MySQL, transcoding names are the same as the corresponding character set names. See Practice #1-9.
|
CAST |
CAST(expr AS type)
Using CAST() function is the same as using CONVERT() function except that it uses keyword AS in between
expr and type rather than a comma.
See Practice #1-10 and Practice #1-11.
|
BINARY |
BINARY str
BINARY is an operator rather than function. It casts a string following it to a binary string.
BINARY str is shorthand for CAST(str AS BINARY)
or CONVERT(expr, BINARY)
This is an easy way to force a column comparison to be done byte by byte rather than character by character. This causes the comparison to be case sensitive even if the column isn't defined as BINARY or BLOB. BINARY also causes trailing spaces to be significant.
See Practice #1-12.
|
Practice #1-1: Using CONVERT function to convert varchar stype to binary type.
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
SELECT CustomerID
FROM orders
WHERE CONVERT(CustomerID, BINARY)='ALFAa';
SELECT CustomerID
FROM orders
WHERE CONVERT(CustomerID, BINARY)='ALFAA ';
Practice #1-2: Using CONVERT function to convert date to string.
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
SELECT CONCAT(CustomerID, ' ordered on ', CONVERT(OrderDate, CHAR(10)))
FROM orders;
Query result set - 830 rows returned:
Practice #1-3: Using CONVERT function to convert string to date type.
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
SELECT CONVERT('1998-03-16 00:00:00', DATE) AS date1,
CONVERT('98-03-16 00:00:00', DATE) AS date2,
CONVERT('19980316', DATE) AS date3,
CONVERT('980316', DATE) AS date4;
Query result set - 1 row returned:
Practice #1-4: Using CONVERT function to convert string to DATETIME type.
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
SELECT CONVERT('1998-03-16', DATETIME) AS datetime1,
CONVERT('98-03-16', DATETIME) AS datetime2,
CONVERT('19980316', DATETIME) AS datetime3,
CONVERT('980316', DATETIME) AS datetime4;
Query result set - 1 row returned:
Practice #1-5: Using CONVERT function to convert double to decimal type.
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
SELECT ProductName, CONVERT(UnitPrice, DECIMAL)
FROM products;
Query result set - 77 rows returned:
Practice #1-7: Using CONVERT function to convert string to time data type.
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
SELECT CONVERT('1998-03-16 21:03:16', TIME) AS time1,
CONVERT('210316', TIME) AS time2;
Query result set - 1 row returned:
Practice #1-8: Using CONVERT function to convert an integer to unsigned integer data type.
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
SELECT CONVERT(0-5, UNSIGNED),
CONVERT(CONVERT(0-5, UNSIGNED), SIGNED);
Query result set - 1 row returned:
Practice #1-9: Using CONVERT function with USING keyword.
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
SELECT CONVERT('abc' USING utf8);
Query result set - 1 row returned:
Practice #1-10: Using CAST function to display internal numeric value for an enum column.
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
SELECT ProductName,
Discontinued AS enum,
CAST(Discontinued AS SIGNED) AS enum_as_int
FROM products
ORDER BY Discontinued;
Query result set - 77 rows returned:
Practice #1-11: Using CAST function in ORDER BY clause.
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
SELECT ProductName,
CAST(Discontinued AS CHAR) AS enum_as_char
FROM products
ORDER BY CAST(Discontinued AS CHAR) DESC;
Query result set - 77 rows returned:
Practice #1-12: Using BINARY operator to convert varchar stype to binary type.
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
SELECT CustomerID
FROM orders
WHERE BINARY CustomerID='ALFAa';
SELECT CustomerID
FROM orders
WHERE BINARY CustomerID='ALFAA ';
Other tutorials in this category
1. Using String Functions, Part 1
2. Using String Functions, Part 2
3. Using String Functions, Part 3
4. Using MySQL Date and Time Functions, Part 1
5. Using MySQL Date and Time Functions, Part 2
6. Using MySQL Date and Time Functions, Part 3
7. Using MySQL Control Flow Functions
8. Using MySQL Comparison Functions
9. Using Aggregate Functions in MySQL
10. MySQL RANK() function
11. MySQL DENSE_RANK() function
12. MySQL ROW_NUMBER() function
13. Compare RANK(), DENSE_RANK(), and ROW_NUMBER() function
14. Using LIMIT and OFFSET to Page Thru Query Result in MySQL
15. Using LIMIT, SQL_CALC_FOUND_ROWS and FOUND_ROWS() to Page Thru Query Result in MySQL
16. Use LEAST function to find the SMALLEST value from multiple arguments
17. How to make case-sensitive comparison in MySQL
18. Use GREATEST function to find the LARGEST value from multiple arguments
19. MySQL MID() function with examples
20. MySQL LOCATE() function with examples
21. MySQL SUBSTR() function with examples
22. MySQL POSITION() function with examples
23. How to use MySQL REPLACE function in SELECT and UPDATE queries
Back to Tutorial Index Page