242

I have months stored in SQL Server as 1,2,3,4,...12. I would like to display them as January,February etc. Is there a function in SQL Server like MonthName(1) = January? I am trying to avoid a CASE statement, if possible.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Saif Khan
  • 18,402
  • 29
  • 102
  • 147

33 Answers33

326

I think this is the best way to get the month name when you have the month number

Select DateName( month , DateAdd( month , @MonthNumber , 0 ) - 1 )

Or

Select DateName( month , DateAdd( month , @MonthNumber , -1 ) )
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
leoinfo
  • 7,860
  • 8
  • 36
  • 48
173

A little hacky but should work:

SELECT DATENAME(month, DATEADD(month, @mydate-1, CAST('2008-01-01' AS datetime)))
Alexander Kojevnikov
  • 17,580
  • 5
  • 49
  • 46
  • 2
    Why the '-1'? Is that needed because the months in SQL Server are offset by one? – DoomerDGR8 Nov 01 '16 at 08:40
  • 2
    @DoomerDGR8 it's actually because the date that's being used to seed the dateadd function starts at 1. if we need January's datename, we would be adding 1 month to 2008-01-01, which would give us 2008-02-01, which is February. so we subtract 1 to account for this, and we get January again. – DForck42 Dec 05 '16 at 16:11
  • To get around the problem of subtracting 1 from your datetime, use a datetime in December as opposed to January. For example SELECT DATENAME(month, DATEADD(month, @mydate, CAST('1978-12-01' AS datetime))) – Steve Matthews Sep 01 '17 at 13:26
  • 6
    This is a good piece of information, but fails to actually answer the question of how to convert a month number to a month name (Rather answers how to get a month name from a date). You've assumed that he has the datetime value rather than just the month number; to get this to work you now need to 'invent' a date/time value. Think the solution from leoinfo was a bit more relevant – schizoid04 Jun 04 '18 at 19:51
109
SELECT DATENAME(month, GETDATE()) AS 'Month Name'
Marcus
  • 12,296
  • 5
  • 48
  • 66
Dharamvir
  • 1,131
  • 1
  • 7
  • 2
85
SUBSTRING('JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC ', (@intMonth * 4) - 3, 3)
Marcus
  • 12,296
  • 5
  • 48
  • 66
Darryl Martin
  • 851
  • 6
  • 2
  • 12
    I like this very left-field alternative way of thinkng! Food for thought – Michael Rodrigues Feb 22 '12 at 04:28
  • 4
    And it's deterministic! Can be used as computed column too, thanks! – Irawan Soetomo Jul 13 '12 at 07:37
  • 1
    nice... i was looking for some simple code to get months from jan to [#] and this worked great. for displaying several months, just change to something like this>> SUBSTRING('JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC ',0, (@intMonth * 4)) – Pablo Contreras Mar 26 '15 at 17:46
  • 2
    I definitely wouldn't think of this as the "right way," but it's a fun way that could be used to solve other problems. – Paul Mar 23 '18 at 14:18
32

Use the Best way

Select DateName( month , DateAdd( month , @MonthNumber , -1 ))
Asif
  • 2,657
  • 19
  • 25
20

It is very simple.

select DATENAME(month, getdate())

output : January

Saeed ur Rehman
  • 727
  • 2
  • 10
  • 25
17

Starting with SQL Server 2012, you can use FORMAT and DATEFROMPARTS to solve this problem. (If you want month names from other cultures, change: en-US)

select FORMAT(DATEFROMPARTS(1900, @month_num, 1), 'MMMM', 'en-US')

If you want a three-letter month:

select FORMAT(DATEFROMPARTS(1900, @month_num, 1), 'MMM', 'en-US')

If you really want to, you can create a function for this:

CREATE FUNCTION fn_month_num_to_name
(
    @month_num tinyint
)
RETURNS varchar(20)
AS
BEGIN
    RETURN FORMAT(DATEFROMPARTS(1900, @month_num, 1), 'MMMM', 'en-US')
END
Paul
  • 3,634
  • 1
  • 18
  • 23
9

You can use the inbuilt CONVERT function

select CONVERT(varchar(3), Date, 100)  as Month from MyTable.

This will display first 3 characters of month (JAN,FEB etc..)

Marcus
  • 12,296
  • 5
  • 48
  • 66
7

The following works for me:

CAST(GETDATE() AS CHAR(3))
Ilyes
  • 14,640
  • 4
  • 29
  • 55
unitario
  • 6,295
  • 4
  • 30
  • 43
7

in addition to original

SELECT DATENAME(m, str(2) + '/1/2011')

you can do this

SELECT DATENAME(m, str([column_name]) + '/1/2011')

this way you get names for all rows in a table. where [column_name] represents a integer column containing numeric value 1 through 12

2 represents any integer, by contact string i created a date where i can extract the month. '/1/2011' can be any date

if you want to do this with variable

DECLARE @integer int;

SET @integer = 6;

SELECT DATENAME(m, str(@integer) + '/1/2011')
6

In some locales like Hebrew, there are leap months dependant upon the year so to avoid errors in such locales you might consider the following solution:

SELECT DATENAME(month, STR(YEAR(GETDATE()), 4) + REPLACE(STR(@month, 2), ' ', '0') + '01')     
Jim Burger
  • 4,399
  • 1
  • 24
  • 27
  • 1
    Is there a function to convert a date to a Jewish date in SQL? Not that I know of... – Hila DG Oct 20 '16 at 03:45
  • function to convert to Jewish date: http://blogs.microsoft.co.il/gerireshef/2011/03/29/%D7%94%D7%9C%D7%95%D7%97-%D7%94%D7%A2%D7%91%D7%A8%D7%99-%D7%91%D7%A2%D7%96%D7%A8%D7%AA-tsql/ – AJ AJ Oct 23 '18 at 10:08
5

Use this statement to convert Month numeric value to Month name.

SELECT CONVERT(CHAR(3), DATENAME(MONTH, GETDATE()))
Ashish Singh
  • 275
  • 3
  • 5
5

Just subtract the current month from today's date, then add back your month number. Then use the datename function to give the full name all in 1 line.

print datename(month,dateadd(month,-month(getdate()) + 9,getdate()))
Geoffrey Fuller
  • 186
  • 1
  • 6
4

Sure this will work

select datename(M,GETDATE())
Ilyes
  • 14,640
  • 4
  • 29
  • 55
Shyam Sa
  • 331
  • 4
  • 8
3
SELECT DateName(M, DateAdd(M, @MONTHNUMBER, -1))
Matt S
  • 14,976
  • 6
  • 57
  • 76
Isaiah
  • 162
  • 2
  • 5
3

To convert month number to month name, try the below

declare @month smallint = 1
select DateName(mm,DATEADD(mm,@month - 1,0))
M2012
  • 3,465
  • 2
  • 20
  • 27
2

i think this is enough to get month name when u have date.

SELECT DATENAME(month ,GETDATE())
fancyPants
  • 50,732
  • 33
  • 89
  • 96
Benazir
  • 63
  • 1
2
SELECT DATENAME(MONTH,dateadd(month, -3,getdate()))
Ilyes
  • 14,640
  • 4
  • 29
  • 55
gvila
  • 29
  • 1
2

You can use the convert functin as below

CONVERT(VARCHAR(3), DATENAME(MM, GETDATE()), 100)
Marcus
  • 12,296
  • 5
  • 48
  • 66
Nori
  • 53
  • 1
1
Declare @MonthNumber int
SET @MonthNumber=DatePart(Month,GETDATE())
Select DateName( month , DateAdd( month , @MonthNumber , 0 ) - 1 )

Explaination:

  1. First Decalre Variable MonthNumber
  2. Get Current Month for DatePart which Return Month Number
  3. Third Query Return Month Name
Tisho
  • 8,320
  • 6
  • 44
  • 52
Wafa Abbas
  • 11
  • 1
1
select monthname(curdate());

OR

select monthname('2013-12-12');
LaurentG
  • 11,128
  • 9
  • 51
  • 66
Piyush
  • 29
  • 1
1

Working for me

SELECT MONTHNAME(<fieldname>) AS "Month Name" FROM <tablename> WHERE <condition>
kleopatra
  • 51,061
  • 28
  • 99
  • 211
1

you can get the date like this. eg:- Users table

id name created_at
1  abc  2017-09-16
2  xyz  2017-06-10

you can get the monthname like this

select year(created_at), monthname(created_at) from users;

output

+-----------+-------------------------------+
| year(created_at) | monthname(created_at)  |
+-----------+-------------------------------+
|      2017        | september              |
|      2017        | june                   |
Janaka Pushpakumara
  • 4,769
  • 5
  • 29
  • 34
1

You can create a function like this to generate the Month and do SELECT dbo.fn_GetMonthFromDate(date_column) as Month FROM table_name


/****** Object:  UserDefinedFunction [dbo].[fn_GetMonthFromDate]    Script Date: 11/16/2018 10:26:33 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[fn_GetMonthFromDate] 
(@date datetime)
RETURNS varchar(50)
AS
BEGIN
    DECLARE @monthPart int

SET @monthPart = MONTH(@date)

IF @monthPart = 1
    BEGIN
        RETURN 'January'
    END
ELSE IF @monthPart = 2
    BEGIN
        RETURN 'February'
    END
ELSE IF @monthPart = 3
    BEGIN
        RETURN 'March'
    END
ELSE IF @monthPart = 4
    BEGIN
        RETURN 'April'
    END
ELSE IF @monthPart = 5
    BEGIN
        RETURN 'May'
    END
ELSE IF @monthPart = 6
    BEGIN
        RETURN 'June'
    END
ELSE IF @monthPart = 7
    BEGIN
        RETURN 'July'
    END
ELSE IF @monthPart = 8
    BEGIN
        RETURN 'August'
    END
ELSE IF @monthPart = 9
    BEGIN
        RETURN 'September'
    END
ELSE IF @monthPart = 10
    BEGIN
        RETURN 'October'
    END
ELSE IF @monthPart = 11
    BEGIN
        RETURN 'November'
    END
ELSE IF @monthPart = 12
    BEGIN
        RETURN 'December'
    END
RETURN NULL END
1

This one worked for me:

@MetricMonthNumber (some number)

SELECT 
(DateName( month , DateAdd( month , @MetricMonthNumber - 1 , '1900-01-01' ) )) AS MetricMonthName
FROM TableName

From a post above from @leoinfo and @Valentino Vranken. Just did a quick select and it works.

bobs
  • 21,844
  • 12
  • 67
  • 78
0

Here is my solution using some information from others to solve a problem.

datename(month,dateadd(month,datepart(month,Help_HelpMain.Ticket_Closed_Date),-1)) as monthname
0

There is no system defined function in SQL server. But you can create your own user-defined function- a scalar function. You would find scalar functions in the Object Explorer for your database: Programmability->Functions->Scalar-valued Functions. Below, I use a table variable to bring it all together.

--Create the user-defined function
CREATE FUNCTION getmonth (@num int)
RETURNS varchar(9) --since 'September' is the longest string, length 9
AS
BEGIN

DECLARE @intMonth Table (num int PRIMARY KEY IDENTITY(1,1), month varchar(9))

INSERT INTO @intMonth VALUES ('January'), ('February'), ('March'), ('April'), ('May')
                           , ('June'), ('July'), ('August') ,('September'), ('October')
                           , ('November'), ('December')

RETURN (SELECT I.month
        FROM @intMonth I
        WHERE I.num = @num)
END
GO

--Use the function for various months
SELECT dbo.getmonth(4) AS [Month]
SELECT dbo.getmonth(5) AS [Month]
SELECT dbo.getmonth(6) AS [Month]
0

This is what I use:

SELECT TRIM(SUBSTRING('January  February March    April    May      June     July     August   SeptemberOctober  November December ', @MonthNumber * 9 - 8,9))
Tom Robinson
  • 1,850
  • 1
  • 15
  • 14
-1
to_char(to_date(V_MONTH_NUM,'MM'),'MONTH')

where V_MONTH_NUM is the month number

SELECT to_char(to_date(V_MONTH_NUM,'MM'),'MONTH')  from dual;
Brian Webster
  • 30,033
  • 48
  • 152
  • 225
-1

Use this statement for getting month name:

DECLARE @date datetime
SET @date='2015/1/4 00:00:00'

SELECT CAST(DATENAME(month,@date )  AS CHAR(3))AS 'Month Name'

This will give you short month name. Like this: Jan, Feb, Mar, etc.

Indian
  • 529
  • 1
  • 12
  • 25
-1

Try this: SELECT MONTHNAME(concat('1970-',[Month int val],'-01'))

For example- SELECT MONTHNAME(concat('1970-',4,'-01'))

The answer is - April

Ajay2707
  • 5,690
  • 6
  • 40
  • 58
Atanu Samanta
  • 145
  • 2
  • 11
-1

If anyone is trying to get the same kind of thing in MySQL. please check below query.

 SELECT MONTH(STR_TO_DATE('November', '%M'))

By this I got required result.

CodeByAk
  • 139
  • 5
-2

Use this statement

SELECT TO_CHAR(current_date,'dd MONTH yyyy') FROM dual

this will convert the month number to month full string

Marcus
  • 12,296
  • 5
  • 48
  • 66
  • 5
    TO_CHAR() is an Oracle function. The OP is using SQL Server, which has the different but equivalent CONVERT function. – APC Sep 28 '11 at 16:04