How to change date format to dd-mm-yyyy in mysql?

by anahi.murazik , in category: SQL , 2 years ago

How to change date format to dd-mm-yyyy in mysql?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by anthony , a year ago

@anahi.murazik In MySQL, you can use the DATE_FORMAT function to change the date format. Here is an example of how you can use it to format a date field called "date_field" in the "table_name" table to the format "dd-mm-yyyy":

1
2
SELECT DATE_FORMAT(date_field, '%d-%m-%Y') as date_field 
FROM table_name;


The %d represents the day of the month (two digits), %m represents the month and %Y represents the year in 4 digits.


If you would like to update this change for all dates in a column and update it for the column, you can use the update statement with the same DATE_FORMAT function

1
2
UPDATE table_name 
SET date_field = DATE_FORMAT(date_field, '%d-%m-%Y');

Member

by natalia , 10 months ago

@anahi.murazik 

You can change the date format to dd-mm-yyyy in MySQL by using the DATE_FORMAT function. Here is an example of how to do this:


1
2
SELECT DATE_FORMAT(date_column, '%d-%m-%Y') AS formatted_date
FROM your_table_name;


In this example, "date_column" is the column name containing the date values you want to format, and "your_table_name" is the name of the table containing the date column. The "%d-%m-%Y" format specifier specifies to display the date in dd-mm-yyyy format.


You can also use the STR_TO_DATE function to convert a string in a specific format to a date. Here is an example:


1
SELECT STR_TO_DATE('31-12-2021', '%d-%m-%Y') AS formatted_date;


In this example, '31-12-2021' is the string that needs to be converted to a date, and '%d-%m-%Y' format specifier specifies that this string is in dd-mm-yyyy format.