diff --git a/client/readline.cc b/client/readline.cc
index 3d524633d69041ec87041803012f62e74eb02a13..52abe1045b72ec2b68fa4960890e40ab6c856973 100644
--- a/client/readline.cc
+++ b/client/readline.cc
@@ -51,7 +51,8 @@ char *batch_readline(LINE_BUFFER *line_buff)
   if (!(pos=intern_read_line(line_buff,&out_length)))
     return 0;
   if (out_length && pos[out_length-1] == '\n')
-    out_length--;				/* Remove '\n' */
+    if (--out_length && pos[out_length-1] == '\r')  /* Remove '\n' */
+      out_length--;                                 /* Remove '\r' */
   line_buff->read_length=out_length;
   pos[out_length]=0;
   return pos;
diff --git a/mysql-test/r/date_formats.result b/mysql-test/r/date_formats.result
index 017631bf8d14dd4790edfbb9bb13893ec19c7a51..cf3495ef26d449b6f112ad7825172babb256aae2 100644
--- a/mysql-test/r/date_formats.result
+++ b/mysql-test/r/date_formats.result
@@ -189,12 +189,12 @@ date	format	datetime
 2003-01-02 02:11:12.12345AM	%Y-%m-%d %h:%i:%S.%f %p	2003-01-02 02:11:12.123450
 2003-01-02 12:11:12.12345 am	%Y-%m-%d %h:%i:%S.%f%p	2003-01-02 00:11:12.123450
 2003-01-02 11:11:12Pm	%Y-%m-%d %h:%i:%S%p	2003-01-02 23:11:12
-10:20:10	%H:%i:%s	0000-00-00 10:20:10
-10:20:10	%h:%i:%s.%f	0000-00-00 10:20:10
-10:20:10	%T	0000-00-00 10:20:10
-10:20:10AM	%h:%i:%s%p	0000-00-00 10:20:10
-10:20:10AM	%r	0000-00-00 10:20:10
-10:20:10.44AM	%h:%i:%s.%f%p	0000-00-00 10:20:10.440000
+10:20:10	%H:%i:%s	0000-00-00 00:00:00
+10:20:10	%h:%i:%s.%f	0000-00-00 00:00:00
+10:20:10	%T	0000-00-00 00:00:00
+10:20:10AM	%h:%i:%s%p	0000-00-00 00:00:00
+10:20:10AM	%r	0000-00-00 00:00:00
+10:20:10.44AM	%h:%i:%s.%f%p	0000-00-00 00:00:00
 15-01-2001 12:59:58	%d-%m-%Y %H:%i:%S	2001-01-15 12:59:58
 15 September 2001	%d %M %Y	2001-09-15 00:00:00
 15 SEPTEMB 2001	%d %M %Y	2001-09-15 00:00:00
@@ -211,6 +211,13 @@ Tuesday 52 2001	%W %V %X	2002-01-01 00:00:00
 15-01-2001	%d-%m-%Y %H:%i:%S	2001-01-15 00:00:00
 15-01-20	%d-%m-%y	2020-01-15 00:00:00
 15-2001-1	%d-%Y-%c	2001-01-15 00:00:00
+Warnings:
+Warning	1292	Incorrect datetime value: '0000-00-00 10:20:10'
+Warning	1292	Incorrect datetime value: '0000-00-00 10:20:10'
+Warning	1292	Incorrect datetime value: '0000-00-00 10:20:10'
+Warning	1292	Incorrect datetime value: '0000-00-00 10:20:10'
+Warning	1292	Incorrect datetime value: '0000-00-00 10:20:10'
+Warning	1292	Incorrect datetime value: '0000-00-00 10:20:10.440000'
 select date,format,DATE(str_to_date(date, format)) as date2 from t1;
 date	format	date2
 2003-01-02 10:11:12	%Y-%m-%d %H:%i:%S	2003-01-02
@@ -251,12 +258,12 @@ date	format	time
 2003-01-02 02:11:12.12345AM	%Y-%m-%d %h:%i:%S.%f %p	02:11:12.123450
 2003-01-02 12:11:12.12345 am	%Y-%m-%d %h:%i:%S.%f%p	00:11:12.123450
 2003-01-02 11:11:12Pm	%Y-%m-%d %h:%i:%S%p	23:11:12
-10:20:10	%H:%i:%s	10:20:10
-10:20:10	%h:%i:%s.%f	10:20:10
-10:20:10	%T	10:20:10
-10:20:10AM	%h:%i:%s%p	10:20:10
-10:20:10AM	%r	10:20:10
-10:20:10.44AM	%h:%i:%s.%f%p	10:20:10.440000
+10:20:10	%H:%i:%s	NULL
+10:20:10	%h:%i:%s.%f	NULL
+10:20:10	%T	NULL
+10:20:10AM	%h:%i:%s%p	NULL
+10:20:10AM	%r	NULL
+10:20:10.44AM	%h:%i:%s.%f%p	NULL
 15-01-2001 12:59:58	%d-%m-%Y %H:%i:%S	12:59:58
 15 September 2001	%d %M %Y	00:00:00
 15 SEPTEMB 2001	%d %M %Y	00:00:00
@@ -273,6 +280,13 @@ Tuesday 52 2001	%W %V %X	00:00:00
 15-01-2001	%d-%m-%Y %H:%i:%S	00:00:00
 15-01-20	%d-%m-%y	00:00:00
 15-2001-1	%d-%Y-%c	00:00:00
+Warnings:
+Warning	1292	Truncated incorrect time value: '0000-00-00 10:20:10'
+Warning	1292	Truncated incorrect time value: '0000-00-00 10:20:10'
+Warning	1292	Truncated incorrect time value: '0000-00-00 10:20:10'
+Warning	1292	Truncated incorrect time value: '0000-00-00 10:20:10'
+Warning	1292	Truncated incorrect time value: '0000-00-00 10:20:10'
+Warning	1292	Truncated incorrect time value: '0000-00-00 10:20:10.440000'
 select date,format,concat(TIME(str_to_date(date, format))) as time2 from t1;
 date	format	time2
 2003-01-02 10:11:12	%Y-%m-%d %H:%i:%S	10:11:12
@@ -282,12 +296,12 @@ date	format	time2
 2003-01-02 02:11:12.12345AM	%Y-%m-%d %h:%i:%S.%f %p	02:11:12.123450
 2003-01-02 12:11:12.12345 am	%Y-%m-%d %h:%i:%S.%f%p	00:11:12.123450
 2003-01-02 11:11:12Pm	%Y-%m-%d %h:%i:%S%p	23:11:12
-10:20:10	%H:%i:%s	10:20:10
-10:20:10	%h:%i:%s.%f	10:20:10
-10:20:10	%T	10:20:10
-10:20:10AM	%h:%i:%s%p	10:20:10
-10:20:10AM	%r	10:20:10
-10:20:10.44AM	%h:%i:%s.%f%p	10:20:10.440000
+10:20:10	%H:%i:%s	NULL
+10:20:10	%h:%i:%s.%f	NULL
+10:20:10	%T	NULL
+10:20:10AM	%h:%i:%s%p	NULL
+10:20:10AM	%r	NULL
+10:20:10.44AM	%h:%i:%s.%f%p	NULL
 15-01-2001 12:59:58	%d-%m-%Y %H:%i:%S	12:59:58
 15 September 2001	%d %M %Y	00:00:00
 15 SEPTEMB 2001	%d %M %Y	00:00:00
@@ -304,6 +318,13 @@ Tuesday 52 2001	%W %V %X	00:00:00
 15-01-2001	%d-%m-%Y %H:%i:%S	00:00:00
 15-01-20	%d-%m-%y	00:00:00
 15-2001-1	%d-%Y-%c	00:00:00
+Warnings:
+Warning	1292	Truncated incorrect time value: '0000-00-00 10:20:10'
+Warning	1292	Truncated incorrect time value: '0000-00-00 10:20:10'
+Warning	1292	Truncated incorrect time value: '0000-00-00 10:20:10'
+Warning	1292	Truncated incorrect time value: '0000-00-00 10:20:10'
+Warning	1292	Truncated incorrect time value: '0000-00-00 10:20:10'
+Warning	1292	Truncated incorrect time value: '0000-00-00 10:20:10.440000'
 select concat('',str_to_date('8:11:2.123456 03-01-02','%H:%i:%S.%f %y-%m-%d'));
 concat('',str_to_date('8:11:2.123456 03-01-02','%H:%i:%S.%f %y-%m-%d'))
 2003-01-02 08:11:02.123456
@@ -574,4 +595,13 @@ DATE_FORMAT('%Y-%m-%d %H:%i:%s', 1151414896)
 NULL
 Warnings:
 Warning	1292	Incorrect datetime value: '%Y-%m-%d %H:%i:%s'
+select str_to_date('04 /30/2004', '%m /%d/%Y');
+str_to_date('04 /30/2004', '%m /%d/%Y')
+2004-04-30
+select str_to_date('04/30 /2004', '%m /%d /%Y');
+str_to_date('04/30 /2004', '%m /%d /%Y')
+2004-04-30
+select str_to_date('04/30/2004 ', '%m/%d/%Y ');
+str_to_date('04/30/2004 ', '%m/%d/%Y ')
+2004-04-30
 "End of 4.1 tests"
diff --git a/mysql-test/r/mysql.result b/mysql-test/r/mysql.result
index 14267afc27ee9c8c8342fb0b2259087f3881fc44..7fd55351880e721ec50f1c6a039a69b2a2fcfceb 100644
--- a/mysql-test/r/mysql.result
+++ b/mysql-test/r/mysql.result
@@ -36,6 +36,8 @@ Tables_in_test
 t1
 t2
 t3
+Tables_in_test
+t1
 _
 Test delimiter : from command line
 a
diff --git a/mysql-test/t/date_formats.test b/mysql-test/t/date_formats.test
index c19ebeb2e2882534f6583d398ffb7d8f67839db9..64bd69c1855fc3af0168e184febe60d898d3666c 100644
--- a/mysql-test/t/date_formats.test
+++ b/mysql-test/t/date_formats.test
@@ -329,4 +329,12 @@ SELECT TIME_FORMAT("25:00:00", '%l %p');
 #
 SELECT DATE_FORMAT('%Y-%m-%d %H:%i:%s', 1151414896);
 
+#
+# Bug #22029: str_to_date returning NULL
+#
+
+select str_to_date('04 /30/2004', '%m /%d/%Y');
+select str_to_date('04/30 /2004', '%m /%d /%Y');
+select str_to_date('04/30/2004 ', '%m/%d/%Y ');
+
 --echo "End of 4.1 tests"
diff --git a/mysql-test/t/events_bugs.test b/mysql-test/t/events_bugs.test
index 6223395bfd9581a5b7d5969a194479dcc916f6f8..605eaa431d0b9e890df0d5da0db822ff8503442c 100644
--- a/mysql-test/t/events_bugs.test
+++ b/mysql-test/t/events_bugs.test
@@ -280,7 +280,7 @@ create event e22830_3 on schedule every 1 hour do alter event e22830_3 on schedu
 create event e22830_4 on schedule every 1 hour do alter event e22830_4 on schedule every (select f22830() from dual) hour;
 select event_name, event_definition, interval_value, interval_field from information_schema.events order by event_name;
 set global event_scheduler=on;
---sleep 0.7
+--sleep 2.4
 set global event_scheduler=off;
 select event_name, event_definition, interval_value, interval_field from information_schema.events order by event_name;
 drop function f22830;
diff --git a/mysql-test/t/mysql_delimiter.sql b/mysql-test/t/mysql_delimiter.sql
index fa80c980b29aa9b88857fd5add50ed47e2e33348..db679c3b06b719e46e42557c7c620a03153cf457 100644
--- a/mysql-test/t/mysql_delimiter.sql
+++ b/mysql-test/t/mysql_delimiter.sql
@@ -49,3 +49,13 @@ delimiter ; # Reset delimiter
 # Bug #11523: \d works differently than delimiter
 #
 source t/mysql_delimiter_source.sql
+delimiter ; # Reset delimiter
+
+#
+# Bug #19799: delimiter command not working correctly when sourcing a sql file
+#             with Windows style line endings.
+#
+source t/mysql_delimiter_19799.sql
+use test//
+show tables//
+delimiter ; # Reset delimiter
diff --git a/mysql-test/t/mysql_delimiter_19799.sql b/mysql-test/t/mysql_delimiter_19799.sql
new file mode 100755
index 0000000000000000000000000000000000000000..2a3d4378492bc99f2aa70a510bc08a4bcda62c3c
--- /dev/null
+++ b/mysql-test/t/mysql_delimiter_19799.sql
@@ -0,0 +1 @@
+delimiter //
diff --git a/sql-common/my_time.c b/sql-common/my_time.c
index ebcf393c49a5bae0637436431fef939790cff79b..839bae51d6496aff5618322aa1f58d3427aeb32b 100644
--- a/sql-common/my_time.c
+++ b/sql-common/my_time.c
@@ -411,7 +411,9 @@ str_to_datetime(const char *str, uint length, MYSQL_TIME *l_time,
   if (number_of_fields < 3 ||
       l_time->year > 9999 || l_time->month > 12 ||
       l_time->day > 31 || l_time->hour > 23 ||
-      l_time->minute > 59 || l_time->second > 59)
+      l_time->minute > 59 || l_time->second > 59 ||
+      (l_time->year == 0 && l_time->month == 0 && l_time->day == 0 && 
+       (l_time->hour != 0 || l_time->minute != 0 || l_time->second != 0)))
   {
     /* Only give warning for a zero date if there is some garbage after */
     if (!not_zero_date)                         /* If zero date */
diff --git a/sql/item_timefunc.cc b/sql/item_timefunc.cc
index 8c9e32a3ce95c7ddfd35b3fa5ea91cba27641613..7231a9eb02744d1934504cf0a6fe2d0ac2d89d1f 100644
--- a/sql/item_timefunc.cc
+++ b/sql/item_timefunc.cc
@@ -290,15 +290,16 @@ static bool extract_date_time(DATE_TIME_FORMAT *format,
 
   for (; ptr != end && val != val_end; ptr++)
   {
+    /* Skip pre-space between each argument */
+    while (val != val_end && my_isspace(cs, *val))
+      val++;
+
     if (*ptr == '%' && ptr+1 != end)
     {
       int val_len;
       char *tmp;
 
       error= 0;
-      /* Skip pre-space between each argument */
-      while (val != val_end && my_isspace(cs, *val))
-	val++;
 
       val_len= (uint) (val_end - val);
       switch (*++ptr) {