Syntax error Get the last days of all the months in MySQL?

Get the last days of all the months in MySQL?



To get the last days of all the months, use the LAST_DAY() function from MySQL −

SELECT LAST_DAY(yourColumnName) from yourTableName;

Let us first create a table −

mysql> create table DemoTable
   (
   ShippingDate date
   );
Query OK, 0 rows affected (0.69 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('2019-01-12');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable values('2019-02-01');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable values('2019-03-04');
Query OK, 1 row affected (0.22 sec)
mysql> insert into DemoTable values('2019-04-21');
Query OK, 1 row affected (0.14 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

+--------------+
| ShippingDate |
+--------------+
| 2019-01-12   |
| 2019-02-01   |
| 2019-03-04   |
| 2019-04-21   |
+--------------+
4 rows in set (0.00 sec)

Following is the query to get the last days of all the months in MySQL −

mysql> select LAST_DAY(ShippingDate) from DemoTable;

Output

+------------------------+
| LAST_DAY(ShippingDate) |
+------------------------+
| 2019-01-31             |
| 2019-02-28             |
| 2019-03-31             |
| 2019-04-30             |
+------------------------+
4 rows in set (0.00 sec)
Updated on: 2019-07-30T22:30:26+05:30

128 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements