
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to get first day of every corresponding month in MySQL?
You can use date-format() function from MySQL to get the first day of every corresponding month. The syntax is as follows −
select DATE_FORMAT(yourDatetimeColumnName ,'%Y-%m-01') as anyVariableName from yourTableName;
To understand the above syntax, let us first create a table. The query to create a table is as follows −
mysql> create table getFirstDayOfMonth -> ( -> DueDatetime datetime -> ); Query OK, 0 rows affected (1.16 sec)
Insert some records in the table using insert command. The query is as follows −
mysql> insert into getFirstDayOfMonth values(date_add(now(),interval 3 month)); Query OK, 1 row affected (0.30 sec) mysql> insert into getFirstDayOfMonth values(date_add(now(),interval -3 month)); Query OK, 1 row affected (0.13 sec) mysql> insert into getFirstDayOfMonth values(date_add(now(),interval -2 month)); Query OK, 1 row affected (0.18 sec) mysql> insert into getFirstDayOfMonth values(date_add(now(),interval -1 month)); Query OK, 1 row affected (0.16 sec) mysql> insert into getFirstDayOfMonth values(date_add(now(),interval -4 month)); Query OK, 1 row affected (0.10 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select *from getFirstDayOfMonth;
Output
+---------------------+ | DueDatetime | +---------------------+ | 2019-03-20 12:32:06 | | 2018-09-20 12:32:16 | | 2018-10-20 12:32:22 | | 2018-11-20 12:32:28 | | 2018-08-20 12:32:33 | +---------------------+ 5 rows in set (0.00 sec)
Here is the query to get the first day of corresponding month. Whatever month is in the column, the following query will return the first day of each of that month −
mysql> select DATE_FORMAT(DueDatetime ,'%Y-%m-01') as DateDemo from getFirstDayOfMonth;
Output
+------------+ | DateDemo | +------------+ | 2019-03-01 | | 2018-09-01 | | 2018-10-01 | | 2018-11-01 | | 2018-08-01 | +------------+ 5 rows in set (0.00 sec)
- Related Articles
- How to get the first day of next month in MySQL?
- How to get the first day of the current month in MySQL?
- How to get the first day of the previous month in MySQL?
- How to get last day of the current month in MySQL?
- How to get last day of the previous month in MySQL?
- How to get last day of the next month in MySQL?
- How to display first day and last day of the month from date records in MySQL?
- Get day name for the corresponding date in MySQL?
- How to get last day of a month in Python?
- How to get the day of the month in JavaScript?
- How to get the same or first day of next month on a given date in Excel?
- How to get first day of week in PHP?
- Get the first and last date of next month in MySQL?
- Get the last day of month in Java
- How to get current day, month and year in Java 8?

Advertisements