
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Cast from Varchar to Int in MySQL
To cast VARCHAR to INT, we can use the cast() function from MySQL. Here is the syntax of cast() function.
cast(anyValue as dataType)
For our example, we will create a table with the help of CREATE command.
mysql> create table VarchartointDemo -> ( -> Value varchar(100) -> ); Query OK, 0 rows affected (0.51 sec)
After creating a table, let us insert some records into the table with the help of INSERT command. The query is as follows ?
mysql> insert into VarchartointDemo values('123'); Query OK, 1 row affected (0.26 sec) mysql> insert into VarchartointDemo values('234'); Query OK, 1 row affected (0.16 sec)
Display all records with the help of select statement.
mysql> select *from VarchartointDemo;
The following is the output.
+-------+ | Value | +-------+ | 123 | | 234 | +-------+ 2 rows in set (0.00 sec)
Cast varchar to int using the following syntax.
SELECT CAST(yourColumnName AS anyDataType) FROM yourTableName;
Apply the above syntax to cast varchar to int.
mysql> SELECT CAST(Value AS UNSIGNED) FROM VarchartointDemo;
The following is the output.
+-------------------------+ | CAST(Value AS UNSIGNED) | +-------------------------+ | 123 | | 234 | +-------------------------+ 2 rows in set (0.00 sec)
Look at the above output, we have changed varchar to int.
Advertisements