Languages
[Edit]
EN

MySQL - DATE_FORMAT() function example

0 points
Created by:
Lia-Perez
565

In this article, we would like to show you DATE_FORMAT() function example in MySQL.

Quick solution

Where:

  • %e - day of the month as a numeric value (0 to 31),
  • %M - full month name,
  • %Y - year as a numeric, 4-digit value.

Note:

Go to the official documentation to find more date format specifiers such as %e, %M and %Y.

Practical examples

Edit

Example 1 - Convert one date

Edit

In this example, we will convert example date to the day month year format.

Query:

Result:

MySQL - convert date to string - result
MySQL - convert date to string - result

You can also specify the separator such as "-" or "/" between day, month and year.

Query:

Result:

MySQL - convert date to string - result
MySQL - convert date to string - result

Example 2 - Convert dates in column

Edit

To show how to convert date to a string, we will use the following table:

MySQL - example data used to convert date to string
MySQL - example data used to convert date to string

Note:

At the end of this article you can find database preparation SQL queries.

In this example, we will display the registration_time column as string in the day month year format.

Query:

Result:

MySQL - format date column to string - result
MySQL - format date column to string - result

Example 3 - Convert dates and time

Edit

In this example, we will display the registration_time column as string in the day month year hour:minute AM/PM format.

Query:

Result:

MySQL - format datetime column to string - result
MySQL - format datetime column to string - result

Where:

  • %h - hour (01 - 12),
  • : - separator between hours and minutes,
  • %i - minutes (00 - 59),
  • %p - AM or PM.

Note:

Go to the official documentation to find more date format specifiers such as %h, %i and %p.

Database preparation

Edit

create_tables.sql file:

insert_data.sql file:

1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

MySQL - dates

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join