w3resource

MySQL Date and Time Exercises: Get the current date in Thursday September 2014 format

MySQL Date Time: Exercise-8 with Solution

Write a MySQL query to get the current date in Thursday September 2014 format.

Sample date : 2014-09-04
Output : September 4, 2014

Code:

-- This SQL query formats the current date and time in a specified format.

SELECT 
    DATE_FORMAT( -- Formats a date and time value according to the specified format.
NOW(), -- Retrieves the current date and time using the NOW() function.
        ''%W %M %Y' -- Specifies the desired format for the date and time: '%W' for the full weekday name, '%M' for the full month name, and '%Y' for the four-digit year.
    );

Explanation:

  • The DATE_FORMAT() function is used to format a date and time value according to the specified format.
  • NOW() retrieves the current date and time.
  • The format string '%W %M %Y' specifies the desired format for the date and time:
    • %W represents the full weekday name (e.g., Monday, Tuesday).
    • %M represents the full month name (e.g., January, February).
    • %Y represents the four-digit year (e.g., 2024).
  • The formatted date and time are returned as the result of the query.

Sample Output:

DATE_FORMAT(NOW(), '%W %M %Y')
Friday August 2017

 

MySQL Code Editor:

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

Previous:Write a MySQL query to get the current date in the specific format.
Next:Write a MySQL query to extract the year from the current date.

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-get-the-current-date-in-the-following-format-1.php