Function Returning Month Name in SQL Server

Hello to everyone,

In this article, I will try to give information about the use of the month name function in SQL Server.

In SQL Server, in some cases, you may want to get the month name from the month number. There are other solutions to this, but let’s create our own function.

You can do this easily using the code below.

CREATE FUNCTION fnMonthInfoFunction
(
    @MonthNumber INT
)
RETURNS VARCHAR(10)
AS
BEGIN
    DECLARE @MonthName VARCHAR(10);
    SELECT @MonthName = CASE
                        WHEN @MonthNumber = 1 THEN
                            'January'
                        WHEN @MonthNumber = 2 THEN
                            'February'
                        WHEN @MonthNumber = 3 THEN
                            'March'
                        WHEN @MonthNumber = 4 THEN
                            'April'
                        WHEN @MonthNumber = 5 THEN
                            'May'
                        WHEN @MonthNumber = 6 THEN
                            'June'
                        WHEN @MonthNumber = 7 THEN
                            'July'
                        WHEN @MonthNumber = 8 THEN
                            'August'
                        WHEN @MonthNumber = 9 THEN
                            'September'
                        WHEN @MonthNumber = 10 THEN
                            'October'
                        WHEN @MonthNumber = 11 THEN
                            'November'
                        WHEN @MonthNumber = 12 THEN
                            'December'
                    END;
    RETURN @MonthName;
END;

--Kullanımı 

SELECT dbo.fnMonthInfoFunction(6) AS MonthNameInfo;
SELECT dbo.fnMonthInfoFunction(3) AS MonthNameInfo;
SELECT dbo.fnMonthInfoFunction(12) AS MonthNameInfo;

When you create the above function and run the code, you will see a result similar to the one below.

Function Returning Month Name in SQL Server

As you can see, when the month number is entered, the month name information is sent to us as a text.

Good luck to everyone in business and life.

319 Views

Yavuz Selim Kart

I try to explain what I know in software and database. I am still improving myself by doing research on many programming languages. Apart from these, I am also interested in Graphic Design and Wordpress. I also have knowledge about SEO and Social media management. In short, I am a determined person who likes to work hard.

You may also like...

Don`t copy text!