 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP 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
Get the type of a variable in MySQL?
You cannot get the type of variable in MySQL. Cast the type of variable into another using CAST operator. The syntax is as follows −
SET @yourVariableName:=’anyValue’
Use the CAST operator to cast to another type. The syntax is as follows −
SELECT CAST( @yourVariableName AS SIGNED);
To understand the above syntax, let us cast to another type.
Case 1: String to unsigned −
mysql> set @StringToInt:='12345'; Query OK, 0 rows affected (0.00 sec)
The query is as follows to another type −
mysql> select CAST(@StringToInt as UNSIGNED);
The following is the output −
+--------------------------------+ | CAST(@StringToInt as UNSIGNED) | +--------------------------------+ | 12345 | +--------------------------------+ 1 row in set (0.00 sec)
Case 2: Int to char
The query is as follows −
mysql> set @IntTochar:=CAST(65 as CHAR); Query OK, 0 rows affected (0.00 sec)
The query is as follows −
mysql> select @IntTochar;
The following is the output −
+------------+ | @IntTochar | +------------+ | 65 | +------------+ 1 row in set (0.00 sec)
Advertisements
 