Convert DATE timestamp to return the month number

MySQLMySQLi Database

To return only the month number, you can use DATE_FORMAT() -

mysql> create table DemoTable1999
   (
   ArrivalDate timestamp
   );
Query OK, 0 rows affected (0.66 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1999 values('2019-01-01 12:34:00');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable1999 values('2019-12-31 10:04:00');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable1999 values('2018-10-11 04:04:30');
Query OK, 1 row affected (0.14 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1999;

This will produce the following output −

+---------------------+
| ArrivalDate         |
+---------------------+
| 2019-01-01 12:34:00 |
| 2019-12-31 10:04:00 |
| 2018-10-11 04:04:30 |
+---------------------+
3 rows in set (0.00 sec)

Here is the query to return month number −

mysql> select date_format(ArrivalDate,'%m') as MonthName from DemoTable1999;

This will produce the following output −

+-----------+
| MonthName |
+-----------+
| 01        |
| 12        |
| 10        |
+-----------+
3 rows in set (0.00 sec)
raja
Published on 02-Jan-2020 05:12:28
Advertisements