How to display first day and last day of the month from date records in MySQL?

MySQLMySQLi Database

Let us first create a table −

mysql> create table DemoTable
   -> (
   -> DueDate date
   -> );
Query OK, 0 rows affected (0.73 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('2019-01-11');
Query OK, 1 row affected (0.08 sec)
mysql> insert into DemoTable values('2019-04-19');
Query OK, 1 row affected (0.14 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+------------+
| DueDate    |
+------------+
| 2019-01-11 |
| 2019-04-19 |
+------------+
2 rows in set (0.00 sec)

Following is the query to display first day and last day of month −

mysql> select date_format(DueDate,'%Y-%m-01') as FirstDayOfMonth,last_day(DueDate) as LastDayOfMonth from DemoTable;

This will produce the following output −

+-----------------+----------------+
| FirstDayOfMonth | LastDayOfMonth |
+-----------------+----------------+
| 2019-01-01      | 2019-01-31     |
| 2019-04-01      | 2019-04-30     |
+-----------------+----------------+
2 rows in set (0.00 sec)
raja
Published on 18-Dec-2019 10:57:28
Advertisements