I'm trying to get the max length a string or integer is set to be on MySQL as a result of running a query. For instance, I have a table like this.
------------------
| ID | Username |
------------------
| 1 | User1 |
| 2 | Admin |
------------------
In the database the ID column is set to int(6)
and the Username column is set to varchar(25)
.
I would like to receive the data type and max length seperately out of a query, like:
INPUT:
SELECT column.name, column.type, column.length FROM myTable
OUTPUT:
----------------------------------
| Name | Type | MaxLength |
----------------------------------
| ID | INT | 6 |
| Username | Varchar | 25 |
----------------------------------
Is this possible at all?
I've tried the following query, but this just returns the data type. Without the amount of characters allowed.
SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'myTable' AND COLUMN_NAME = 'myColumn';
I've also tried this query SELECT COLUMN_NAME, COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS where TABLE_SCHEMA = 'my_db_name' AND TABLE_NAME = 'myTable';
However this one does return the name and type with the lenght. But the length is returned like this, varchar(25)
. I feel like there should be an easier way to retrieve the '25' than to seperate it manually from the string.