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. - With Date
- With Month Number
Before that , very simple query to select month number in sql server select MONTH(GETDATE()) as MonthSeq Output 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
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 The both output is same, but ways are different.so now you know to select month from sql server in two different ways. ConclusionIn 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. |