使用STR_TO_DATE()方法插入日期记录,如以下语法所示:
select str_to_date(yourColumnName,'%b %Y') from yourTableName;
让我们首先创建一个表-
mysql> create table DemoTable -> ( -> JoiningYear varchar(20) -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('Jan 2018'); mysql> insert into DemoTable values('May 2107'); mysql> insert into DemoTable values('Aug 2019'); mysql> insert into DemoTable values('Oct 2020');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+-------------+ | JoiningYear | +-------------+ | Jan 2018 | | May 2107 | | Aug 2019 | | Oct 2020 | +-------------+ 4 rows in set (0.00 sec)
这是在MySQL中插入日期记录的查询-
mysql> select str_to_date(JoiningYear,'%b %Y') from DemoTable;
这将产生以下输出-
+----------------------------------+ | str_to_date(JoiningYear,'%b %Y') | +----------------------------------+ | 2018-01-00 | | 2107-05-00 | | 2019-08-00 | | 2020-10-00 | +----------------------------------+ 4 rows in set (0.05 sec)