diff --git a/executor/insert_test.go b/executor/insert_test.go index b8f92f0e4a55a..6350ae78237b0 100644 --- a/executor/insert_test.go +++ b/executor/insert_test.go @@ -172,7 +172,7 @@ func (s *testSuite) TestInsertZeroYear(c *C) { tk.MustQuery(`select * from t1;`).Check(testkit.Rows( `0`, `0`, - `2000`, + `0`, `2000`, )) } diff --git a/types/convert_test.go b/types/convert_test.go index 3d58fc19bb56d..b5f3f737be0d5 100644 --- a/types/convert_test.go +++ b/types/convert_test.go @@ -619,6 +619,9 @@ func (s *testTypeConvertSuite) TestConvert(c *C) { signedDeny(c, mysql.TypeYear, 123, "") signedDeny(c, mysql.TypeYear, 3000, "") signedAccept(c, mysql.TypeYear, "2000", "2000") + signedAccept(c, mysql.TypeYear, "abc", "0") + signedAccept(c, mysql.TypeYear, "00abc", "2000") + signedAccept(c, mysql.TypeYear, "0019", "2019") // time from string signedAccept(c, mysql.TypeDate, "2012-08-23", "2012-08-23") diff --git a/types/datum.go b/types/datum.go index a16563f5b07d5..880e6312bcf97 100644 --- a/types/datum.go +++ b/types/datum.go @@ -1172,11 +1172,14 @@ func (d *Datum) convertToMysqlYear(sc *stmtctx.StatementContext, target *FieldTy ) switch d.k { case KindString, KindBytes: - y, err = StrToInt(sc, d.GetString()) + s := d.GetString() + y, err = StrToInt(sc, s) if err != nil { return ret, errors.Trace(err) } - fromStr = true + if len(s) != 4 && len(s) > 0 && s[0:1] == "0" { + fromStr = true + } case KindMysqlTime: y = int64(d.GetMysqlTime().Time.Year()) case KindMysqlDuration: