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;

Explanation:

  • DAY(joining_date) extracts the day component from the joining_date column.
  • The result displays each employee’s name, joining date, and the extracted day.

2. Filtering Records Based on Day

Suppose we want to find employees who joined on the 10th of any month. We can use the DAY() function in a WHERE clause:

</>
Copy
SELECT name, joining_date
FROM employees
WHERE DAY(joining_date) = 10;

Explanation:

  • This query filters employees whose joining_date falls on the 10th day of any month.
  • In our sample data, Arjun joined on 2024-06-10, so his record will be retrieved.

Conclusion

The SQL DAY() function is a used for extracting the day component from a date value. In this tutorial, we covered:

  1. The syntax and usage of the DAY() function.
  2. How to retrieve the day from a column in a table.
  3. Filtering records based on a specific day.