Jump to content

20/20 hindsight - date problem


AndyB

Recommended Posts

A problem with legacy data (lots of it, unfortunately) in a MySQL database that needs to be sorted by date except that the 'date' field is actually just a varchar field generated thus:

[code]$dated = gmdate("h:i a"). "[GMT] on ". gmdate("F d Y")[/code]

I'm hoping that the query can be constructed with 'magic' that someone can suggest will work with that date data and php4.4.2/MySQL 4.1.21-standard
Link to comment
Share on other sites

By way of clarification, an example of the 'date' field is [b]12:05 pm [GMT] on September 06 2006[/b].

I don't want to re-write the 'real' date back to the database, I *only* want an SQL query that can sort the data by yyyy-mm-dd when the database field contains a string like the example above.
Link to comment
Share on other sites

[code]
SELECT
*
FROM
tablename
ORDER BY
STR_TO_DATE(SUBSTRING(date_column, LOCATE('on', date_column) + 3), '%M %d %Y')
[/code]

You should be able to set the substring start position to a hardcoded 19 but I'm not sure.
[code]
STR_TO_DATE(SUBSTRING(date_column, 19), '%M %d %Y')
[/code]

It may be faster to sort this in PHP with usort.
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.