How to Get Month Name in T-SQL

Posted By  RRaveen On 12 Nov 2010 21:11:54
emailbookmarkadd commentsprint
No of Views:1017
Bookmarked:0 times
Votes:0 times

Introduction

 In this tips will explain to you, two different ways to select month name in sql server.i'm going to work in two different ways.

  1. With Date
  2. With Month Number

Before that , very simple query to select month number in sql server

select MONTH(GETDATE()) as MonthSeq

Output 

Image Loading

Next we will try to get month name, as mentioned earlier there is many ways. So I will give all the ways below.


1.    Using DATENAME inbuilt function
 

select DATENAME(m,GETDATE()) as 'Month Name'

The DATENAME is function is great function, this function accepts the two arguments at the same time, support different types of argument for the first argument. The second argument always dates. It achieve date related T-SQL. Here I have used to for Month name selection with “M/MM”-Month.


Output 

Image Loading

Note:More about DATENAME function: http://msdn.microsoft.com/en-us/library/ms174395.aspx

2. Using DATENAME inbuilt function with month number.

Sometimes we know only month number, so that time how could take the Month name use month number.like below,

DECLARE @monthNo smallint
SET @monthNo = 11
SELECT DateName(m,DATEADD(m,@monthNo,-1)) as 'Month Name'

Output 

Image Loading

The both output is same, but ways are different.so now you know to select month from sql server in two different ways.

Conclusion

In this tips we have learned how to get the month from sql server with two different ways.

  • With Date
  • With month number

Thank you for reading, if you have any comments, Please post in the below of article.

Sign Up to vote for this article
Other popular Tips/Tricks
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top