Jacek
Jacek

Reputation: 12053

Month name in SQL Server

Is there a build-in function in SQL Server which return my polish (or any other language) name of month by month number? Or need I write my own function?

For example

GetMonthName(11) => Listopad (November)

Upvotes: 3

Views: 17158

Answers (1)

HTTP 410
HTTP 410

Reputation: 17608

You can use the function Month or datepart "month" to return a month number from date/time. Then you can use the DATENAME function:

SELECT DATENAME(month, GETDATE()) AS 'Month Name'

The return value depends on the language environment set by using SET LANGUAGE and by the "configure the default language" server configuration option for the login.

So as per Jacek's comment, this can be summarised as:

SET LANGUAGE Polish SELECT DATENAME(month, GETDATE()) AS 'Month Name'

If literally all you have is a month number, you would need to build a datetime variable that incorporates this month number so that you can apply the above approach. In SQL Server 2012, you can use DATETIMEFROMPARTS:

SELECT @RandomDate = DATETIMEFROMPARTS(2012, @MonthNumber, 1, 1, 1, 1, 0) 
SET LANGUAGE Polish SELECT DATENAME(month, @RandomDate) AS 'Month Name'

Upvotes: 9

Related Questions