Skip to content

SQL-511:- Convert Month Number to Month Name Function in SQL #339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
-- Create sample table
CREATE TABLE sales_data (
id INT,
month_number INT,
amount DECIMAL(10,2)
);

-- Insert sample data
INSERT INTO sales_data VALUES
(1, 1, 1500.00), (2, 3, 2200.50), (3, 7, 1895.75), (4, 12, 3100.00);

-- Convert month number to month name using CASE statement
SELECT
id,
month_number,
CASE month_number
WHEN 1 THEN 'January' WHEN 2 THEN 'February' WHEN 3 THEN 'March'
WHEN 4 THEN 'April' WHEN 5 THEN 'May' WHEN 6 THEN 'June'
WHEN 7 THEN 'July' WHEN 8 THEN 'August' WHEN 9 THEN 'September'
WHEN 10 THEN 'October' WHEN 11 THEN 'November' WHEN 12 THEN 'December'
END AS month_name,
amount
FROM sales_data;
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-- Create sample table
CREATE TABLE sales_data (
id INT,
month_number INT,
amount DECIMAL(10,2)
);

-- Insert sample data
INSERT INTO sales_data VALUES
(1, 1, 1500.00), (2, 3, 2200.50), (3, 7, 1895.75), (4, 12, 3100.00);

-- Create a custom function to convert month number to month name
CREATE FUNCTION dbo.MonthName (@month_number INT)
RETURNS VARCHAR(20)
AS
BEGIN
RETURN DATENAME(MONTH, DATEFROMPARTS(2000, @month_number, 1))
END;

-- Invoking the custom function
SELECT
id,
month_number,
dbo.MonthName(month_number) AS month_name,
amount
FROM sales_data;
18 changes: 18 additions & 0 deletions sql-queries-dates/convert-month-number-to-name/using-datename.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-- Create sample table
CREATE TABLE sales_data (
id INT,
month_number INT,
amount DECIMAL(10,2)
);

-- Insert sample data
INSERT INTO sales_data VALUES
(1, 1, 1500.00), (2, 3, 2200.50), (3, 7, 1895.75), (4, 12, 3100.00);

-- Convert month number to month name using the DATENAME function
SELECT
id,
month_number,
DATENAME(MONTH, DATEFROMPARTS(2000, month_number, 1)) AS month_name,
amount
FROM sales_data;
18 changes: 18 additions & 0 deletions sql-queries-dates/convert-month-number-to-name/using-format.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-- Create sample table
CREATE TABLE sales_data (
id INT,
month_number INT,
amount DECIMAL(10,2)
);

-- Insert sample data
INSERT INTO sales_data VALUES
(1, 1, 1500.00), (2, 3, 2200.50), (3, 7, 1895.75), (4, 12, 3100.00);

-- Convert month number to month name using the FORMAT function
SELECT
id,
month_number,
FORMAT(DATEFROMPARTS(2000, month_number, 1), 'MMMM') AS month_name,
amount
FROM sales_data;