将MySQL中的日期时间转换为月份名?
要仅获取月份名,语法如下 –
select date_format(yourColumnName,'%M %Y') from yourTableName;
首先让我们创建一个表 –
mysql> create table DemoTable1619
-> (
-> ArrivalTime datetime
-> );
Query OK ,0行受影响(0.45秒)
使用insert命令在表中插入一些记录 –
mysql> insert into DemoTable1619 values(now());
Query OK ,1行受影响(0.40秒)
mysql> insert into DemoTable1619 values(curdate());
Query OK ,1行受影响(0.15秒)
mysql> insert into DemoTable1619 values('2019-12-31');
Query OK ,1行受影响(0.17秒)
使用select语句显示表中的所有记录 –
mysql> select * from DemoTable1619;
这将生成以下输出 –
+---------------------+
| ArrivalTime |
+---------------------+
| 2019-10-20 15:02:12 |
| 2019-10-20 00:00:00 |
| 2019-12-31 00:00:00 |
+---------------------+
3 rows in set (0.00 sec)
以下是在MySQL中将日期时间转换为月份名称的查询 –
mysql> select date_format(ArrivalTime,'%M %Y') from DemoTable1619;
这将生成以下输出 –
+----------------------------------+
| date_format(ArrivalTime,'%M %Y') |
+----------------------------------+
| October 2019 |
| October 2019 |
| December 2019 |
+----------------------------------+
3 rows in set (0.00 sec)
阅读更多:MySQL 教程