使用str_to_date()方法-
select month(str_to_date(yourColumnName,'%b')) from yourTableName;
让我们首先创建一个表-
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, MonthName varchar(100) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable(MonthName) values('Jan'); mysql> insert into DemoTable(MonthName) values('Mar'); Display all records from the table using select statement: mysql> select *from DemoTable;
输出结果
+----+-----------+ | Id | MonthName | +----+-----------+ | 1 | Jan | | 2 | Mar | +----+-----------+ 2 rows in set (0.00 sec)
以下是将字符串转换为月份(数字)的查询-
mysql> select month(str_to_date(MonthName,'%b')) from DemoTable;
输出结果
+------------------------------------+ | month(str_to_date(MonthName,'%b')) | +------------------------------------+ | 1 | | 3 | +------------------------------------+ 2 rows in set (0.05 sec)