Bug #81638 Function substr with order by gives incorrect result
Submitted: 30 May 2016 7:40 Modified: 16 Dec 2019 22:46
Reporter: Su Dylan Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: DML Severity:S2 (Serious)
Version:5.7.8, 5.5.49, 5.6.30, 5.7.12 OS:Any
Assigned to: CPU Architecture:Any

[30 May 2016 7:40] Su Dylan
Description:
Output:
===
mysql> create table t1(c1 bigint);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t1 values(23057),(33006);
ct c1,Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select c1, substr(c1, 2, 9) from t1 order by 2;
+-------+------------------+
| c1    | substr(c1, 2, 9) |
+-------+------------------+
| 33006 | 3006             |
| 23057 | 3057             |
+-------+------------------+
2 rows in set (0.00 sec)

mysql> select c1, substr(c1, 2, 10) from t1 order by 2;
+-------+-------------------+
| c1    | substr(c1, 2, 10) |
+-------+-------------------+
| 23057 | 3057              |
| 33006 | 3006              |
+-------+-------------------+
2 rows in set (0.00 sec)

mysql> select version();
+--------------+
| version()    |
+--------------+
| 5.7.8-rc-log |
+--------------+
1 row in set (0.00 sec)

Problem:
====
The last query gives incorrect order, in which 3006 should be before 3057.

How to repeat:

drop table t1;
create table t1(c1 bigint);
insert into t1 values(23057),(33006);
select c1, substr(c1, 2, 9) from t1 order by 2;
select c1, substr(c1, 2, 10) from t1 order by 2;

Suggested fix:
The last query gives incorrect order, in which 3006 should be before 3057.
[30 May 2016 8:01] MySQL Verification Team
Hello Su Dylan,

Thank you for the report and test case.

Thanks,
Umesh
[16 Dec 2019 22:46] Roy Lyseng
Posted by developer:
 
Fixed in 5.7.29