Jump to content

[SOLVED] sorting date format


williejoe

Recommended Posts

I have an output that is a date in the format like this: Nov 23, 2007 which comes from my MySQL database and is formatted using my recordset.  I would like to sort the list of dates with newest first, so I put a line in my recordset that says: ORDER BY 'date' DESC

The problem is that it sorts on the first letter of the month and not the actual date.  How can I fix this?

Link to comment
Share on other sites

The only effective way to sort or compare dates is if they are in the form of most significant field to least significant field - ie. year then month then day or if the are converted to a form where later dates have higher values, such as a Unix timestamp. This is why the standard sql DATE data type is yyyy-mm-dd.

 

You need to store your dates in a database as a DATE data type to allow sorts and comparisons to directly work.

Link to comment
Share on other sites

The 'date' column in my database is formatted like this: 2007-12-25 and in my recordset I have this:

DATE_FORMAT(date, '%M %e, %Y') AS date

which formats the output into this format: Dec 25, 2007.

I could change the DATE_FORMAT line so that the output was year-month-day, but I would rather leave it the other way because I think it is easier to read.  Is there a way to format the output so I can sort it or is there a way to alter the recordset to get this? 

Link to comment
Share on other sites

Your ORDER BY is apparently operating on your formatted output with the alias name date instead of your column named date (I would think this would generate an error because the reference is ambiguous.) I recommend picking an alias name that is different than a column name.

 

Once you get your query to ORDER BY your column named date, it should work the way you expect.

Link to comment
Share on other sites

That's my cousin trying to be helpful.  I think I found my problem without him.  I forgot to tie the date column to the new dynamic field.  That is, I had the date column tied to 'date' and not 'newdate'.  Thanks guys, and (cousin) Roger, I didn't need you after all.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.