w3resource

MySQL Date and Time Exercises: Display the current date in the specified format

MySQL Date Time: Exercise-13 with Solution

Write a MySQL query to display the current date in a given format.
Sample output : 05/09/2014

Code:

-- This SQL query formats the current date in a specific date format.

SELECT 
date_format( -- Formats a date value according to the specified format.
CURDATE(), -- Retrieves the current date using the CURDATE() function.
        '%d/%m/%Y' -- Specifies the desired format for the date.
    );

Explanation:

  • The date_format() function in MySQL is used to format a date value according to the specified format.
  • CURDATE() retrieves the current date.
  • The format string '%d/%m/%Y' specifies the desired format for the date:
    • %d represents the day of the month with leading zeros (e.g., 01, 02, 03...).
    • %m represents the month with leading zeros (e.g., 01 for January, 02 for February...).
    • %Y represents the four-digit year (e.g., 2024).
  • The formatted date is returned as the result of the query.

Sample Output:

date_format(CURDATE(),'%d/%m/%Y')
11/08/2017

 

MySQL Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous:Write a MySQL query to display the current date in the spacific format.
Next:Write a MySQL query to display the current date in the specified format.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/mysql-exercises/date-time-exercises/write-a-query-to-display-the-current-date-in-the-following-format-3.php