Skip to content

Commit 9a2332b

Browse files
authored
Merge pull request #339 from kapilbk1996/SQL-511
SQL-511:- Convert Month Number to Month Name Function in SQL
2 parents 6dc794a + 08c75c3 commit 9a2332b

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
-- Create sample table
2+
CREATE TABLE sales_data (
3+
id INT,
4+
month_number INT,
5+
amount DECIMAL(10,2)
6+
);
7+
8+
-- Insert sample data
9+
INSERT INTO sales_data VALUES
10+
(1, 1, 1500.00), (2, 3, 2200.50), (3, 7, 1895.75), (4, 12, 3100.00);
11+
12+
-- Convert month number to month name using CASE statement
13+
SELECT
14+
id,
15+
month_number,
16+
CASE month_number
17+
WHEN 1 THEN 'January' WHEN 2 THEN 'February' WHEN 3 THEN 'March'
18+
WHEN 4 THEN 'April' WHEN 5 THEN 'May' WHEN 6 THEN 'June'
19+
WHEN 7 THEN 'July' WHEN 8 THEN 'August' WHEN 9 THEN 'September'
20+
WHEN 10 THEN 'October' WHEN 11 THEN 'November' WHEN 12 THEN 'December'
21+
END AS month_name,
22+
amount
23+
FROM sales_data;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
-- Create sample table
2+
CREATE TABLE sales_data (
3+
id INT,
4+
month_number INT,
5+
amount DECIMAL(10,2)
6+
);
7+
8+
-- Insert sample data
9+
INSERT INTO sales_data VALUES
10+
(1, 1, 1500.00), (2, 3, 2200.50), (3, 7, 1895.75), (4, 12, 3100.00);
11+
12+
-- Create a custom function to convert month number to month name
13+
CREATE FUNCTION dbo.MonthName (@month_number INT)
14+
RETURNS VARCHAR(20)
15+
AS
16+
BEGIN
17+
RETURN DATENAME(MONTH, DATEFROMPARTS(2000, @month_number, 1))
18+
END;
19+
20+
-- Invoking the custom function
21+
SELECT
22+
id,
23+
month_number,
24+
dbo.MonthName(month_number) AS month_name,
25+
amount
26+
FROM sales_data;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-- Create sample table
2+
CREATE TABLE sales_data (
3+
id INT,
4+
month_number INT,
5+
amount DECIMAL(10,2)
6+
);
7+
8+
-- Insert sample data
9+
INSERT INTO sales_data VALUES
10+
(1, 1, 1500.00), (2, 3, 2200.50), (3, 7, 1895.75), (4, 12, 3100.00);
11+
12+
-- Convert month number to month name using the DATENAME function
13+
SELECT
14+
id,
15+
month_number,
16+
DATENAME(MONTH, DATEFROMPARTS(2000, month_number, 1)) AS month_name,
17+
amount
18+
FROM sales_data;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-- Create sample table
2+
CREATE TABLE sales_data (
3+
id INT,
4+
month_number INT,
5+
amount DECIMAL(10,2)
6+
);
7+
8+
-- Insert sample data
9+
INSERT INTO sales_data VALUES
10+
(1, 1, 1500.00), (2, 3, 2200.50), (3, 7, 1895.75), (4, 12, 3100.00);
11+
12+
-- Convert month number to month name using the FORMAT function
13+
SELECT
14+
id,
15+
month_number,
16+
FORMAT(DATEFROMPARTS(2000, month_number, 1), 'MMMM') AS month_name,
17+
amount
18+
FROM sales_data;

0 commit comments

Comments
 (0)