Updated September 28, 2023
Introduction to SQL Current Month
SQL current month is retrieved using the functions available in SQL that are related to date and time, some of which include the MONTH() function and GETDATE() function that can be used togetherly to get the month value of the current month or alternatively, we can make the use of DATEPART() function that can be used to get the value of the current month the month value from current date that is retrieved from the GETDATE() function. Manier times, we might face the situation in SQL where we need to know the value of the current month and use it to apply the constraint in the query for retrieving data for the only the current month or with reference to the current month. In this case, we first need to fetch the value of the current month. In this article, we will learn about the retrieval of the current month value in SQL using different methodologies and functions and how we can apply the conditions in the query statement to use the current month value as the restriction in it.
Table of Contents
Retrieving Data for the Current Month
Let’s look at some common methods for retrieving data for the current month.
1. Using GETDATE() function
In SQL, we use the GETDATE() method to retrieve the value of the current date, which is today’s date. Let us try executing a simple query statement using which we will retrieve the value of the current date using the GETDATE() function. Following will be our query statement to do so.
SELECT getdate() AS 'Present Date';
The output of the execution of the above query statement is displayed in the following image, which gives the current date as well as time –
2. Using MONTH() function
Now, in order to fetch only the month value from the whole time and date string that is retrieved from the GETDATE() function, we can simply use the MONTH() function which retrieves the month value from the passed parameter’s date value. As we have to retrieve the month value of today which is the current month value., we will pass the GETDATE() function as a parameter or the value of GETDATE() function as the parameter. Consider the following query statement –
SELECT MONTH(getdate()) AS "Present Month";
In the above query statement, we have passed the whole function call of GETDATE() as a parameter to the MONTH() function. Hence, the output of the execution of the above query statement retrieves the current month, as shown in the below image –
Alternatively, we can even supply the literal value retrieved as the output of GETDATE() function in our first query as a parameter to MONTH() function to get the month value. Consider the following query statement for the above-mentioned case –
SELECT MONTH(2020-07-04) AS "Month of the provided date";
The output of the above query statement is as follows –
From the output, we can observe that the function retrieves the month value from the date passed to it. The date when this article was written is 2020-07-04. You can pass your date or use the GETDATE() function as a parameter to get the current value of the month using SQL query statements.
3. Using DATEPART() function
We can alternatively use DATEPART() function in SQL that helps us retrieve the specific parts of the date such as day, month, year, etc from the supplied date. In order to retrieve the month value of the current date, we can pass the GETDATE() function as a parameter. The DATEPART() function accepts two parameters, with the first parameter specifying the value to be retrieved. It can have value such as MONTH< DAY, YEAR, etc and the second parameter is the date value that is to be split and searched for the value for which you are using the DATEPART() function. We can use the DATEPART() function to retrieve the MONTH value by using the following query statement –
select DATEPART(MONTH, GETDATE()) as "Present Month Of the Year";
The output of the execution of the above query statement is as shown in the below image –
We can see that it retrieves the same output, which is 12, as it represents the current month value.
Getting the Name of the Current Month
In case, if we want to fetch the value of the name of the current month instead of its numerical value, we can make the use of the SQL functions such as FORMAT() or DATENAME() to do so. Let us first discuss the FORMAT() function. The FORMAT() function accepts two parameters: the date from which you want to retrieve the month value, and the second parameter is the format in which you want to display the name of the month. For example, if you want to print only the three characters of the month in the beginning then the format will be “MMM” and in case if you want to print the whole month name then the format needs to be specified as “MMMM”. For retrieving the current month, we need to pass the current date as the first parameter, which can be sent using the GETDATE() function. We will use the following query statement to get the current month name –
SELECT FORMAT(GETDATE(),'MMMM') AS Month;
The output of the execution of the above query statement is as shown in the below image –
Similarly, we can use the DATENAME function to print the name of the month by using the SQL query. We can use the following query statement to get the value of the current month name using DATENAME() function –
SELECT DATENAME(month, GETDATE()) AS Month;
The output of the execution of the above query statement is as shown in the below image –
We can also use the MONTH function to retrieve the data of only the current month if there is any field in the table that stores the date of the transaction, we can compare the month of that date to the current month by using the following restriction MONTH(DateStoredInField) = MONTH(GETDATE());
Conclusion
We can retrieve the current month value in SQL using the MONTH() and DATEPART() functions along with the GETDATE() function. In SQL, we use functions like DATENAME() and FORMAT() to retrieve the name of the month.
Recommended Articles
We hope that this EDUCBA information on “SQL Current Month” was beneficial to you. You can view EDUCBA’s recommended articles for more information.