=== modified file 'mysql-test/r/ps_2myisam.result'
--- mysql-test/r/ps_2myisam.result	2011-07-23 06:47:28 +0000
+++ mysql-test/r/ps_2myisam.result	2013-06-13 13:59:20 +0000
@@ -1793,7 +1793,7 @@ t5	CREATE TABLE `t5` (
   `param08` longtext,
   `const09` datetime DEFAULT NULL,
   `param09` longblob,
-  `const10` decimal(22,6) NOT NULL DEFAULT '0.000000',
+  `const10` decimal(22,6) DEFAULT NULL,
   `param10` decimal(65,30) DEFAULT NULL,
   `const11` int(4) DEFAULT NULL,
   `param11` bigint(20) DEFAULT NULL,
@@ -1823,7 +1823,7 @@ def	test	t5	t5	const08	const08	253	19	19
 def	test	t5	t5	param08	param08	252	4294967295	19	Y	16	0	8
 def	test	t5	t5	const09	const09	12	19	19	Y	128	0	63
 def	test	t5	t5	param09	param09	252	4294967295	19	Y	144	0	63
-def	test	t5	t5	const10	const10	246	24	16	N	1	6	63
+def	test	t5	t5	const10	const10	246	24	16	Y	0	6	63
 def	test	t5	t5	param10	param10	246	67	40	Y	0	30	63
 def	test	t5	t5	const11	const11	3	4	4	Y	32768	0	63
 def	test	t5	t5	param11	param11	8	20	4	Y	32768	0	63

=== modified file 'mysql-test/r/ps_3innodb.result'
--- mysql-test/r/ps_3innodb.result	2011-07-23 06:47:28 +0000
+++ mysql-test/r/ps_3innodb.result	2013-06-13 14:00:27 +0000
@@ -1776,7 +1776,7 @@ t5	CREATE TABLE `t5` (
   `param08` longtext,
   `const09` datetime DEFAULT NULL,
   `param09` longblob,
-  `const10` decimal(22,6) NOT NULL DEFAULT '0.000000',
+  `const10` decimal(22,6) DEFAULT NULL,
   `param10` decimal(65,30) DEFAULT NULL,
   `const11` int(4) DEFAULT NULL,
   `param11` bigint(20) DEFAULT NULL,
@@ -1806,7 +1806,7 @@ def	test	t5	t5	const08	const08	253	19	19
 def	test	t5	t5	param08	param08	252	4294967295	19	Y	16	0	8
 def	test	t5	t5	const09	const09	12	19	19	Y	128	0	63
 def	test	t5	t5	param09	param09	252	4294967295	19	Y	144	0	63
-def	test	t5	t5	const10	const10	246	24	16	N	1	6	63
+def	test	t5	t5	const10	const10	246	24	16	Y	0	6	63
 def	test	t5	t5	param10	param10	246	67	40	Y	0	30	63
 def	test	t5	t5	const11	const11	3	4	4	Y	32768	0	63
 def	test	t5	t5	param11	param11	8	20	4	Y	32768	0	63

=== modified file 'mysql-test/r/ps_4heap.result'
--- mysql-test/r/ps_4heap.result	2011-07-23 06:47:28 +0000
+++ mysql-test/r/ps_4heap.result	2013-06-13 13:59:33 +0000
@@ -1777,7 +1777,7 @@ t5	CREATE TABLE `t5` (
   `param08` longtext,
   `const09` datetime DEFAULT NULL,
   `param09` longblob,
-  `const10` decimal(22,6) NOT NULL DEFAULT '0.000000',
+  `const10` decimal(22,6) DEFAULT NULL,
   `param10` decimal(65,30) DEFAULT NULL,
   `const11` int(4) DEFAULT NULL,
   `param11` bigint(20) DEFAULT NULL,
@@ -1807,7 +1807,7 @@ def	test	t5	t5	const08	const08	253	19	19
 def	test	t5	t5	param08	param08	252	4294967295	19	Y	16	0	8
 def	test	t5	t5	const09	const09	12	19	19	Y	128	0	63
 def	test	t5	t5	param09	param09	252	4294967295	19	Y	144	0	63
-def	test	t5	t5	const10	const10	246	24	16	N	1	6	63
+def	test	t5	t5	const10	const10	246	24	16	Y	0	6	63
 def	test	t5	t5	param10	param10	246	67	40	Y	0	30	63
 def	test	t5	t5	const11	const11	3	4	4	Y	32768	0	63
 def	test	t5	t5	param11	param11	8	20	4	Y	32768	0	63

=== modified file 'mysql-test/r/ps_5merge.result'
--- mysql-test/r/ps_5merge.result	2011-07-23 06:47:28 +0000
+++ mysql-test/r/ps_5merge.result	2013-06-13 14:00:02 +0000
@@ -1713,7 +1713,7 @@ t5	CREATE TABLE `t5` (
   `param08` longtext,
   `const09` datetime DEFAULT NULL,
   `param09` longblob,
-  `const10` decimal(22,6) NOT NULL DEFAULT '0.000000',
+  `const10` decimal(22,6) DEFAULT NULL,
   `param10` decimal(65,30) DEFAULT NULL,
   `const11` int(4) DEFAULT NULL,
   `param11` bigint(20) DEFAULT NULL,
@@ -1743,7 +1743,7 @@ def	test	t5	t5	const08	const08	253	19	19
 def	test	t5	t5	param08	param08	252	4294967295	19	Y	16	0	8
 def	test	t5	t5	const09	const09	12	19	19	Y	128	0	63
 def	test	t5	t5	param09	param09	252	4294967295	19	Y	144	0	63
-def	test	t5	t5	const10	const10	246	24	16	N	1	6	63
+def	test	t5	t5	const10	const10	246	24	16	Y	0	6	63
 def	test	t5	t5	param10	param10	246	67	40	Y	0	30	63
 def	test	t5	t5	const11	const11	3	4	4	Y	32768	0	63
 def	test	t5	t5	param11	param11	8	20	4	Y	32768	0	63
@@ -5067,7 +5067,7 @@ t5	CREATE TABLE `t5` (
   `param08` longtext,
   `const09` datetime DEFAULT NULL,
   `param09` longblob,
-  `const10` decimal(22,6) NOT NULL DEFAULT '0.000000',
+  `const10` decimal(22,6) DEFAULT NULL,
   `param10` decimal(65,30) DEFAULT NULL,
   `const11` int(4) DEFAULT NULL,
   `param11` bigint(20) DEFAULT NULL,
@@ -5097,7 +5097,7 @@ def	test	t5	t5	const08	const08	253	19	19
 def	test	t5	t5	param08	param08	252	4294967295	19	Y	16	0	8
 def	test	t5	t5	const09	const09	12	19	19	Y	128	0	63
 def	test	t5	t5	param09	param09	252	4294967295	19	Y	144	0	63
-def	test	t5	t5	const10	const10	246	24	16	N	1	6	63
+def	test	t5	t5	const10	const10	246	24	16	Y	0	6	63
 def	test	t5	t5	param10	param10	246	67	40	Y	0	30	63
 def	test	t5	t5	const11	const11	3	4	4	Y	32768	0	63
 def	test	t5	t5	param11	param11	8	20	4	Y	32768	0	63

=== modified file 'mysql-test/r/type_datetime_hires.result'
--- mysql-test/r/type_datetime_hires.result	2011-06-09 15:23:39 +0000
+++ mysql-test/r/type_datetime_hires.result	2013-06-13 14:13:49 +0000
@@ -338,3 +338,15 @@ select * from t1;
 a	b
 2010-01-02 03:04:05.000000	2010-01-02 03:04:05
 drop table t1;
+#
+# MDEV-4651 Crash in my_decimal2decimal in a ORDER BY query
+#
+SET @@time_zone='+00:00';
+CREATE TABLE t1 (a DATETIME(4) NOT NULL);
+INSERT INTO t1 VALUES ('2001-01-01 00:00:00'),('2000-00-00 00:00:00');
+SELECT UNIX_TIMESTAMP(a) FROM t1 ORDER BY 1;
+UNIX_TIMESTAMP(a)
+NULL
+978307200.0000
+DROP TABLE t1;
+SET @@time_zone=DEFAULT;

=== modified file 'mysql-test/suite/maria/r/ps_maria.result'
--- mysql-test/suite/maria/r/ps_maria.result	2011-07-23 06:47:28 +0000
+++ mysql-test/suite/maria/r/ps_maria.result	2013-06-13 14:00:14 +0000
@@ -1793,7 +1793,7 @@ t5	CREATE TABLE `t5` (
   `param08` longtext,
   `const09` datetime DEFAULT NULL,
   `param09` longblob,
-  `const10` decimal(22,6) NOT NULL DEFAULT '0.000000',
+  `const10` decimal(22,6) DEFAULT NULL,
   `param10` decimal(65,30) DEFAULT NULL,
   `const11` int(4) DEFAULT NULL,
   `param11` bigint(20) DEFAULT NULL,
@@ -1823,7 +1823,7 @@ def	test	t5	t5	const08	const08	253	19	19
 def	test	t5	t5	param08	param08	252	4294967295	19	Y	16	0	8
 def	test	t5	t5	const09	const09	12	19	19	Y	128	0	63
 def	test	t5	t5	param09	param09	252	4294967295	19	Y	144	0	63
-def	test	t5	t5	const10	const10	246	24	16	N	1	6	63
+def	test	t5	t5	const10	const10	246	24	16	Y	0	6	63
 def	test	t5	t5	param10	param10	246	67	40	Y	0	30	63
 def	test	t5	t5	const11	const11	3	4	4	Y	32768	0	63
 def	test	t5	t5	param11	param11	8	20	4	Y	32768	0	63

=== modified file 'mysql-test/t/type_datetime_hires.test'
--- mysql-test/t/type_datetime_hires.test	2011-06-09 15:23:39 +0000
+++ mysql-test/t/type_datetime_hires.test	2013-06-13 14:13:15 +0000
@@ -69,3 +69,12 @@ alter table t1 modify b datetime, modify
 select * from t1;
 drop table t1;
 
+--echo #
+--echo # MDEV-4651 Crash in my_decimal2decimal in a ORDER BY query
+--echo #
+SET @@time_zone='+00:00';
+CREATE TABLE t1 (a DATETIME(4) NOT NULL);
+INSERT INTO t1 VALUES ('2001-01-01 00:00:00'),('2000-00-00 00:00:00');
+SELECT UNIX_TIMESTAMP(a) FROM t1 ORDER BY 1;
+DROP TABLE t1;
+SET @@time_zone=DEFAULT;

=== modified file 'sql/item_timefunc.h'
--- sql/item_timefunc.h	2012-12-28 12:41:46 +0000
+++ sql/item_timefunc.h	2013-06-13 07:24:20 +0000
@@ -370,6 +370,7 @@ class Item_func_seconds_hybrid: public I
       decimals= args[0]->decimals;
     set_if_smaller(decimals, TIME_SECOND_PART_DIGITS);
     max_length=17 + (decimals ? decimals + 1 : 0);
+    set_persist_maybe_null(1);
   }
   void find_num_type() { hybrid_type= decimals ? DECIMAL_RESULT : INT_RESULT; }
   double real_op() { DBUG_ASSERT(0); return 0; }

