I have this scenario, it looks like MySQL is taking the largest decimal value and tries to cast the other values to that.
The problem is that this query is generated by an external library, so I don't have control over this code, at this level at least.
Do you have some idea how to fix this?
SELECT 20 AS x
UNION SELECT null
UNION SELECT 2.2;
+------+
| x |
+------+
| 9.9 | -- why from 20 to 9.9
| NULL |
| 2.2 |
+------+
Expected Result
+------+
| x |
+------+
| 20 | -- or 20.0, doesn't matter really in my case
| NULL |
| 2.2 |
+------+
Adding more context, I'm using Entity Framework 6 with an extension library http://entityframework-extensions.net/ to save changes in batches, specifically the method context.BulkSaveChanges();, this library creates queries using "select union".
Best Answer
Looks like a bug to me and I can confirm this puzzling behaviour in:
If possible you can cast the integer value to a double:
or make sure you have the double value first:
Further observations after reading the comments in @Evan Carroll's answer
Ok, using int values does not seem to produce the error.
ERROR: Seems like output is decimal(2,1)
The error is not isolated to the command line interface, it exists for python2-mysql-1.3.12-1.fc27.x86_64 as well:
Oddly enough the error disappears if null is moved first or last:
If null is placed first, resulting type is decimal(20,1). If null is placed last resulting type is decimal(3,1)
The error also disappears if another leg is added to the union:
resulting type decimal(20,1)
adding another null in the middle preserves the error:
But adding a null at the beginning fixes it:
As expected casting first value to decimal(3,1) works.
Finally, explicitly casting to decimal(2,1) produces the same error but with a warning: