SQL DAY()
The SQL DAY() function is used to extract the day from a given date value. It returns an integer representing the day of the month (1 to 31) from a date or datetime column.
In this tutorial, we will explore the SQL DAY() function, its syntax, and how to use it with practical examples.
Syntax of SQL DAY Function
The basic syntax of the SQL DAY() function is as follows:
</>
Copy
SELECT DAY(date_column) FROM table_name;
Alternatively, you can use it with a specific date:
</>
Copy
SELECT DAY('2025-02-15') AS day_of_month;
The DAY() function extracts the day part of the given date. For instance, if the date is 2025-02-15, the function returns 15.
Step-by-Step Examples Using SQL DAY Function
1 Extracting Day from a Column
Let’s create an employees table to demonstrate the DAY() function:
</>
Copy
CREATE TABLE employees (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50),
joining_date DATE
);
Insert some sample data:
</>
Copy
INSERT INTO employees (name, joining_date)
VALUES
('Arjun', '2024-06-10'),
('Ram', '2023-12-25'),
('Priya', '2025-03-05');
Now, let’s extract the day from each employee’s joining date:
</>
Copy
SELECT name, joining_date, DAY(joining_date) AS day_of_joining
FROM employees;
