Search code examples
mysqlcommand-lineformattingscientific-notation

Disable scientific notation in MySQL command-line client?


I have a MySQL table with many numeric columns (some INT, some FLOAT). I would like to query it with the MySQL command-line client (specifically, mysql Ver 14.14 Distrib 5.1.41, for debian-linux-gnu (x86_64) using readline 6.1), like so:

SELECT * FROM table WHERE foo;

Unfortunately, if the value of any numeric field exceeds 10^6, this client displays the result in scientific notation, which makes reading the results difficult.

I could correct the problem by FORMAT-ing each of the fields in my query, but there are many of them and many tables I would like to query. Instead I'm hoping to find a client variable or flag I can set to disable scientific notation for all queries.

I have not been able to find one in the --help or the man page, nor searching Google or this site. Instead all I find are discussions of preserving/removing scientific notation when using <insert-programming-language>'s MySQL API.

Thank you for any tips.

::edit::

Here's an example table ...

mysql> desc foo;
+--------------+-------------+------+-----+-------------------+
| Field        | Type        | Null | Key | Default           |
+--------------+-------------+------+-----+-------------------+
| date         | date        | NO   | PRI | NULL              |
| name         | varchar(20) | NO   | PRI | NULL              |
| val          | float       | NO   |     | NULL              |
| last_updated | timestamp   | NO   |     | CURRENT_TIMESTAMP |
+--------------+-------------+------+-----+-------------------+

and some example values ...

mysql> select * from foo where date='20120207';
+------------+--------+--------------+---------------------+
| date       | name   | val          | last_updated        |
+------------+--------+--------------+---------------------+
| 2012-02-07 | A      |      88779.5 | 2012-02-07 13:38:14 |
| 2012-02-07 | B      |  1.00254e+06 | 2012-02-07 13:38:14 |
| 2012-02-07 | C      |      78706.5 | 2012-02-07 13:38:15 |
+------------+--------+--------------+---------------------+

Now, the actual values I loaded into the third field are:

88779.5, 1002539.25, 78706.5390625

and they can be seen exactly if I manipulate the value:

mysql> select date, name, ROUND(val, 10), last_updated from foo where ...
+------------+---+--------------------+---------------------+
| 2012-02-07 | A |   88779.5000000000 | 2012-02-07 13:38:14 |
| 2012-02-07 | B | 1002539.2500000000 | 2012-02-07 13:38:14 |
| 2012-02-07 | C |   78706.5390625000 | 2012-02-07 13:38:15 |

Something in the client seems to be enforcing that I only be allowed to see six significant figures, even though there are more in the table.

If a query such as

mysql> select ROUND(*, 2) from foo ...

were possible, that would be great! Otherwise I can't really take the time to individually wrap 100 column names in "ROUND()" whenever I need to inspect some data.

Interestingly, I occasionally use a phpMyAdmin interface to browse the contents of some of these tables, and that interface also has this 6 significant figure limitation. So it's not limited to just the CLI.


Solution

  • Well, after reading the documentation more thoroughly, I still can't see any reason why a client would limit itself to displaying only 6 sig figs from a FLOAT (especially when the table itself is definitely storing more).

    Nonetheless, an acceptable solution (for this weary user) is to change all my tables to use DECIMAL(16,4) instead of FLOAT. Unfortunately, this makes all my numbers show up with 4 decimal places (even if they're all '0'). But at least all numbers have the same width now, and my client never displays them in scientific notation or limits the number of sig figs in its output.