Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the type of a variable in MySQL

Tags:

sql

mysql

If I define a variable like set @a = "1";. How can I see that @a is a string?

like image 479
Adam B Avatar asked Aug 03 '09 16:08

Adam B


People also ask

How do I find the data type of a variable in SQL?

You can also use the describe statement to check the datatypes of all the columns of the table as follows: DESCRIBE table_name; If you are interested in SQL, then check out this SQL Course by Intellipaat that offers instructor-led training, certification, and job assistance.

How do I view a variable in MySQL?

SHOW VARIABLES accepts an optional GLOBAL or SESSION variable scope modifier: With a GLOBAL modifier, the statement displays global system variable values. These are the values used to initialize the corresponding session variables for new connections to MySQL. If a variable has no global value, no value is displayed.

What is MySQL data type?

The data type is a guideline for SQL to understand what type of data is expected inside of each column, and it also identifies how SQL will interact with the stored data. In MySQL there are three main data types: string, numeric, and date and time.

What is @@ in MySQL?

@@ - System Variable @@ is used for system variables. Using different suffix with @@ , you can get either session or global value of the system variable.


1 Answers

You CAN determine the type of a variable in MySQL. Create a table by selecting your variable and then check the column type:

set @a:="1"; -- your variable

drop temporary table if exists foo;
create temporary table foo select @a; -- dirty magic
desc foo;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| @a    | longtext | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
like image 172
5lava Avatar answered Oct 31 '22 13:10

5lava