Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format date in SELECT * query

Tags:

mysql

Is it possible to format a date field in a SELECT * query? I want to get all of the columns from my table but need a different format from the date.

Something like:

SELECT *, FORMATDATE(date,'%m-%d-%y') AS date FROM my_table;

or do I have to process it in php after the query?

like image 791
PGrau Avatar asked Jan 31 '12 21:01

PGrau


2 Answers

Use DATE_FORMAT:

SELECT *, DATE_FORMAT(date, "%m-%d-%y") AS date FROM my_table;
like image 191
unutbu Avatar answered Sep 23 '22 09:09

unutbu


select DATE_FORMAT(your_date_field_name,'%m-%d-%y') AS whatever FROM your_table_name_here;

the result is something like this

09-22-11

as opposed to this

2011-02-02 15:42:51

like image 31
Average Joe Avatar answered Sep 24 '22 09:09

Average Joe