EN
MySQL - convert value to CHAR (using CAST() function)
0 points
In this article, we would like to show you how to convert a value to CHAR
type using CAST()
function in MySQL.
Quick solution:
xxxxxxxxxx
1
SELECT CAST(value AS CHAR);
In this example, we will convert INT
value to CHAR
type.
Query:
xxxxxxxxxx
1
SELECT CAST(100 AS CHAR) AS 'int_to_char';
Result:

To show how to convert value to CHAR, we will use the following values
table:

Note:
At the end of this article you can find database preparation SQL queries.
In this example, we will convert max_value
column (which is INT
type) from values
table to CHAR
type.
Query:
xxxxxxxxxx
1
SELECT
2
`id`, `min_value`, `max_value`,
3
CAST(`max_value` AS CHAR) AS 'max_value_char'
4
FROM `values`;
Output:

create_tables.sql
file:
xxxxxxxxxx
1
CREATE TABLE `values` (
2
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
3
`min_value` INT,
4
`max_value` INT,
5
PRIMARY KEY (`id`)
6
);
insert_data.sql
file:
xxxxxxxxxx
1
INSERT INTO `values`
2
(`min_value`, `max_value`)
3
values
4
(1000, 1500),
5
(1500, 2000),
6
(1400, 2500),
7
(1200, 3000),
8
(1800, 3500);