hive cast string to date in 'dd/MMM/yyyy' format order by and group by issue
I think you're mixing up the formatting or display of data, with the underlying data itself. If the table stores a date as a string formatted in one manner, [27/Feb/2016:00:24:31 +0530]
it's still a string, and strings sort differently than actual dates, timestamps, or numbers.
Ideally, you would store the date as a TIMESTAMP
datatype. When you want to display it, use DATE_FORMAT
, and when you want to sort it, use ORDER BY
on the underlying data field. So if your field is of type TIMESTAMP
called some_time
, you could query as
SELECT DATE_FORMAT(some_time, 'dd/MMM/yyyy')FROM some_tableWHERE some_conditionORDER BY some_time DESC
If you're stuck with a string that's stored as a valid timestamp value, then you'll have to do more work, perhaps
SELECT SUBSTR(some_time, 2, 11)FROM some_tableWHERE some_conditionORDER BY unix_timestamp(SUBSTR(some_time,2,11), 'dd/MMM/yyyy'))
The second option displays the value as desired, and orders by a number -- a unix timestamp is just a number, but it has the same order as the date, so no need to cast that further to an actual date.