0

In PL/pgsql,pg_type() give use data type of a variable. As Example:

select pg_typeof(3) as type_of;
-- output: integer
select pg_typeof(true) as type_of;
-- output: boolean

Similarly, how to check datatype of a variable in MySQL ?

1
  • Provide an example where you "need" such. Maybe we can help you find a workaround.
    – Rick James
    Mar 20 at 20:56

1 Answer 1

1

There is no built-in function to return the type of a variable or expression in MySQL. You can review the list of built-in functions here: https://dev.mysql.com/doc/refman/8.0/en/built-in-function-reference.html

The closest thing I can think of is to run the mysql command-line client with the --column-type-info option.

% mysql --column-type-info

mysql> select 3, true;
Field   1:  `3`
Catalog:    `def`
Database:   ``
Table:      ``
Org_table:  ``
Type:       LONGLONG
Collation:  binary (63)
Length:     2
Max_length: 1
Decimals:   0
Flags:      NOT_NULL BINARY NUM 

Field   2:  `true`
Catalog:    `def`
Database:   ``
Table:      ``
Org_table:  ``
Type:       LONGLONG
Collation:  binary (63)
Length:     1
Max_length: 1
Decimals:   0
Flags:      NOT_NULL BINARY NUM 


+---+------+
| 3 | true |
+---+------+
| 3 |    1 |
+---+------+
1 row in set (0.00 sec)

But this output is only in the MySQL client. You can't use the result in SQL expressions as if it were a built-in function.

2
  • Yes, right. We can see tables column type through commands desc <table_name> <column_nam>; or show columns from <table_name> like '<column_nam>';
    – Art Bindu
    Mar 23 at 19:54
  • @ArtBindu Thanks for your comment! Please feel free to post an answer of your own. Mar 24 at 2:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.