

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Is there a way to convert Integer field into Varchar without losing data in MySQL?
You can use ALTER command to convert Integer into Varchar. Let us first create a table
mysql> create table DemoTable ( UserId int, UserFirstName varchar(20), UserLastName varchar(20), UserAge int ); Query OK, 0 rows affected (0.73 sec)
Now check the description of table using DESC command:
mysql> desc DemoTable;
This will produce the following output −
+---------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------------+-------------+------+-----+---------+-------+ | UserId | int(11) | YES | | NULL | | | UserFirstName | varchar(20) | YES | | NULL | | | UserLastName | varchar(20) | YES | | NULL | | | UserAge | int(11) | YES | | NULL | | +---------------+-------------+------+-----+---------+-------+ 4 rows in set (0.17 sec)
Now you can change the integer data type to varchar without losing data:
mysql> alter table DemoTable change UserId UserId varchar(20); Query OK, 0 rows affected (1.35 sec) Records: 0 Duplicates: 0 Warnings: 0
Check the table description:
mysql> desc DemoTable;
This will produce the following output −
+---------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------------+-------------+------+-----+---------+-------+ | UserId | varchar(20) | YES | | NULL | | | UserFirstName | varchar(20) | YES | | NULL | | | UserLastName | varchar(20) | YES | | NULL | | | UserAge | int(11) | YES | | NULL | | +---------------+-------------+------+-----+---------+-------+ 4 rows in set (0.00 sec)
- Related Questions & Answers
- Convert varchar to unsigned integer in MySQL
- In MySQL, is there a way to turn column records into a list?
- Is there a way in MySQL to reverse a boolean field with a single query?
- Searching for an integer value in a varchar field in MySQL?
- Convert VARCHAR data to MySQL date format?
- Is there a way to know your current username in MySQL?
- How to change the column position of MySQL table without losing column data?
- Is there any way to embed a PDF file into an HTML5 page?
- What is JavaScript’s highest integer value that a Number can go to without losing precision?
- Sorting varchar field numerically in MySQL?
- Convert varchar to date in MySQL?
- Is there a way to select a value which matches partially in MySQL?
- Is there an easy way to rename a table in a MySQL procedure?
- Is there a way to list collections in MongoDB?
- Is there a way to make a list from a MySQL table in Java?
Advertisements