Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select distinct timestamp as DD/MM/YYYY mysql

Tags:

sql

mysql

I have multiple rows for itemDate (timestamp) like below:

itemDate
2013-04-09 17:24:31
2013-04-09 10:24:31
2013-04-08 12:20:30

When I use SELECT DISTINCT(itemDate) FROM item;, I am getting two rows for 2013-04-08.

I want to get the date of different date (based on YYYY-MM-DD) ignoring whether the time is different for the same day.

The result that I need should be like the following, when I query from that table:

itemDate
2013-04-09
2013-04-08
like image 724
kitokid Avatar asked Apr 10 '13 01:04

kitokid


3 Answers

That's because those values are different. If you're trying to do this by date then use the DATE() function:

SELECT DISTINCT(DATE(itemDate)) FROM item;
like image 140
John Conde Avatar answered Oct 24 '22 19:10

John Conde


An alternative from the other answer is to use DATE_FORMAT()

SELECT DATE_FORMAT(itemDate, '%Y-%m-%d') ItemDate 
FROM   Item
GROUP  BY DATE_FORMAT(itemDate, '%Y-%m-%d')

http://www.sqlfiddle.com/#!2/bfaf5e/3

like image 37
Skinny Pipes Avatar answered Oct 24 '22 19:10

Skinny Pipes


On my side, I have tried the different options suggested above but the Sybase DB I use is rejecting me:

Error: Function 'DATE' not found. If this is a SQLJ function or SQL function, use sp_help to check whether the object exists (sp_help may produce a large amount of output).

My colleague gave me this and works OK

SELECT distinct(CONVERT(varchar(20), timestamp, 111)) FROM HistoOrder

I use 111 but you can find code list here http://www.w3schools.com/sql/func_convert.asp

like image 33
JayB Avatar answered Oct 24 '22 19:10

JayB