Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting a distinct date by day only from datetime (YYYY-MM-DD HH:MM:SS) in MySQL

Tags:

sql

mysql

Executing this command brings me the following (all the dates from all columns, so it essentially did the same thing as SELECT date without distinct):

SELECT DISTINCT date FROM daily ORDER BY date DESC

2013-02-12 16:40:52
2013-02-06 11:48:49
2013-02-06 11:36:41
2013-02-06 11:35:59
2013-02-04 19:38:12
2013-02-04 18:12:30
2013-02-04 09:58:41
2013-02-04 09:43:01
2013-02-04 09:35:51
2013-02-04 09:30:22
2013-02-04 09:24:57
2013-02-04 09:21:09
2013-02-04 08:50:13

What I need:

2013-02-12
2013-02-06
2013-02-04
  1. Is there any way to alter my date table and convert it to YYYY-MM-DD instead?

  2. If not, is there a way to select distinct dates based only on the day?

like image 981
eveo Avatar asked Feb 13 '13 00:02

eveo


2 Answers

mysql> select DATE_FORMAT(Current_Timestamp, '%c %d %Y') from dual;
+--------------------------------------------+
| DATE_FORMAT(Current_Timestamp, '%c %d %Y') |
+--------------------------------------------+
| 2 12 2013                                  |
+--------------------------------------------+
1 row in set (0.01 sec)

mysql> 

of course you would be using your 'daily' table.

mysql> select DATE_FORMAT(Date, '%c %d %Y') from daily;

or maybe you want

mysql> select * from daily group by DATE_FORMAT(Date, '%c %d %Y');
like image 134
Michael Durrant Avatar answered Oct 20 '22 07:10

Michael Durrant


Try this one:

SELECT DISTINCT(CONVERT(VARCHAR, CONVERT(DATETIME,[DATE]),23)) AS DT  
FROM DAILY
ORDER BY DT ASC
like image 2
Nilesh Nikumbh Avatar answered Oct 20 '22 06:10

Nilesh Nikumbh