Connecting Tech Pros Worldwide Help | Site Map

Mysql Order By Date is having some issues...

  #1  
Old July 3rd, 2009, 01:39 PM
Newbie
 
Join Date: Jul 2009
Posts: 1
Hi , I have a site..where i need to list records based on the modified date...im storing the date in the table like this "2 July, 2009, 8:37 am"....and the query i write is "SELECT * FROM Table ORDER By profile_updated_date DESC"

profile_updated_field will have values similar to 2 July, 2009, 8:37 am....and the listed records order is not correct...the data type for this fields is varchar...what could be the issue..i tried many way to solve this..please suggestion a solution..
Really appreciate your help...

Regards,
Unni
  #2  
Old July 4th, 2009, 01:06 AM
Member
 
Join Date: Aug 2007
Posts: 119

re: Mysql Order By Date is having some issues...


Well, it seems to me that if you have a field that holds dates and times, then you might want to think about using a datetime type instead of a varchar. Varchar is going to sort in alphanumeric order exactly the way they are written which puts"1 Dec" before "1 Nov" before "1 Oct" and so on. Datetime will sort in the order you are expecting.
  #3  
Old July 4th, 2009, 01:03 PM
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,701
Provided Answers: 4

re: Mysql Order By Date is having some issues...


Hi.

I'd have to agree with Annalyzer.

Dates should always be stored using the date types: Date, Time, DateTime or Timestamp.
Unless you have a very very very good reason not to.

You should never store dates as strings (VarChar, Char, Text).
If you store them as strings (or integers, as some people seem to think is best) you will have to manually handle how they are sorted, compared, etc... because MySQL will simply look at them as strings (or numbers) and treat them accordingly.

So my advice to you is: convert your string date fields into real date types.
Then MySQL will sort them for you.
Reply

Tags
date order by mysql sort


Similar Threads
Thread Thread Starter Forum Replies Last Post
A dream of more functional CLP in v8.3 sopranos2@gmail.com answers 38 June 9th, 2006 07:25 PM
Enough RAM for entire Database.. cost aside, is this going to be fastest? Andy B answers 19 November 23rd, 2005 01:25 AM
MySQL 4.0.14 has been released Lenz Grimmer answers 0 July 19th, 2005 11:45 PM
PHP or COLDFUSION English Teacher answers 60 July 17th, 2005 02:39 AM