MySQL DAY() Function Example

MySQL DAY() Function Example

MySQL DAY() function; In this tutorial, we will learn how to use MySQL day() function with the help of examples.

MySQL DAY() Function

The MySQL day() is used to return the day of the month from a given date. the DAY() function is a synonym for the DAYOFMONTH() function.

The “day of the month” is the value between 1 and 31. For example, if you provide a date of 2020-08-03, then the DAY () function 3 will return.

Syntax

The DAY() function syntax is:

DAY(date)

The date here is the date value that you want the day of the month from which you returned.

Example-1

Now we take an example to demonstrate.

SELECT DAY('2020-06-18') AS 'Result';

Output-1

+--------+
| Result |
+--------+
|     18 |
+--------+

Example-2

If there is a leading zero in the part of the day, then the leading zero has been left out of the result.

SELECT DAY('2018-02-01') AS 'Result';

Output-2

+--------+
| Result |
+--------+
|      1 |
+--------+

Example-3

Next database example, we take an example of removing part of the day with a column when running the query against the database.

SELECT
  created_at AS created_date,
  DAY(created_at) AS day_of_month
FROM users
WHERE where= 1;

Output-3

+---------------------+--------------+
| created date        | day_of_month |
+---------------------+--------------+
| 2010-08-23 10:33:39 |           23 |
+---------------------+--------------+

Example-4 Current Date/Time

Let’s take another example , extracting part of the day from the current date and time (which is now returned using the () function).

  SELECT 
  NOW(),
  DAY(NOW());

Output-4

+---------------------+------------+
| NOW()               | DAY(NOW()) |
+---------------------+------------+
| 2019-07-10 18:30:44 |         10 |
+---------------------+------------+

Example-4 CURDATE() Function

Let’s take a another example of using CURDATE() function. Basically CURDATE() function returns only the date without time.

SELECT 
CURDATE(),
DAY(CURDATE());    

Output-5

+------------+----------------+
| CURDATE()  | DAY(CURDATE()) |
+------------+----------------+
| 2019-05-15 |             15 |
+------------+----------------+

Conclusion

Here, You have learned how to use mysql day() function with various examples.

Recommended MySQL Tutorials

If you have any questions or thoughts to share, use the comment form below to reach us.

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *