我的工作是在数据库中创建一个具有正确数据类型的新表。以下是样本记录:
RegisteredMonthYear
------------------------
May 2011
March 1998
January 2000
在我插入转换后的值之前,我尝试使用它来转换它STR_TO_DATE()
以检查值是否正确以及结果是否完全不是我想要的。这是我的查询:
SELECT RegisteredMonthYear,
STR_TO_DATE(RegisteredMonthYear, '%M %Y') NewDate,
STR_TO_DATE(CONCAT(RegisteredMonthYear, ' 01'), '%M %Y %d') newDate2,
STR_TO_DATE(RegisteredMonthYear, '%M %Y') + INTERVAL 1 DAY newDate3
FROM TableName
+---------------------+---------------------------------+--------------------------------+----------+
| REGISTEREDMONTHYEAR | NEWDATE | NEWDATE2 | NEWDATE3 |
+---------------------+---------------------------------+--------------------------------+----------+
| May 2011 | April, 30 2011 00:00:00+0000 | May, 01 2011 00:00:00+0000 | (null) |
| March 1998 | February, 28 1998 00:00:00+0000 | March, 01 1998 00:00:00+0000 | (null) |
| January 2000 | December, 31 1999 00:00:00+0000 | January, 01 2000 00:00:00+0000 | (null) |
+---------------------+---------------------------------+--------------------------------+----------+
见这里演示:http ://www.sqlfiddle.com/#!2/89a67/7
如您所见,列NEWDATE
落后了一天。为什么会出现这样的结果?
当我尝试01
在列中连接字符串时NEWDATE2
,结果符合预期。回到NEWDATE
专栏,我试图添加一天,认为它会在专栏中给出准确的值,NEWDATE3
但结果是NULL
.
对此有任何想法吗?