EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials SQL Tutorial SQL Current Month

SQL Current Month

SQL Current Month

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.

Using MONTH() and GETDATE() function to fetch current month

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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 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 as displayed in the following image which gives the current date as well as time –

SQL Current Month 1

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 –

SQL Current Month 2

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 –

SQL Current Month 3

We can observe from the output that the retrieved month value is from the date which was passed to it. 2020-07-04 is the date when this article was being written. You can pass your date or use GETDATE() function as parameter to get the current value of the month using SQL query statements.

Using DATEPART() function and GETDATE() 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 parameter. The DATEPART() function accepts two parameters, the first parameter is the value that is 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 –

SQL Current Month 4

We can see that the same output that is 12 is retrieved as that is 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 which are the date from which you want to retrieve the month value and the second parameter will be the format in which the name of the month is to be displayed. 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 –

output

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 –

output 1

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. To retrieve the name of the month functions in SQL such as DATENAME() and FORMAT() are used.

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.

  1. SQL Ranking Function
  2. SQL Server Replace
  3. SQL DATEADD()
  4. SQL Rename Table
MICROSOFT POWER BI Training
48+ Hours of HD Videos
8 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
CYBER SECURITY & ETHICAL HACKING Certification Course
89+ Hours of HD Videos
13 Courses
3 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
MICROSOFT AZURE
97+ Hours of HD Videos
15 Courses
12 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
KALI LINUX Certification Course
26+ Hours of HD Videos
6 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
SQL - Everything in SQL
 253+ Hours of HD Videos
51 Courses
6 Mock Tests & Quizzes
  Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more