| re: Converting VARCHAR "date" info to an actual date field
>(Sigh.) This means that the so-called date fields look like this :[color=blue]
>
>August 1, 2005, 9:09 am EDT
>October 13, 2004, 12:28 pm EDT
>
>This makes them essentially useless for sorting and logical evaluation.
>Understandbly, the MySQL date functions don't seem able to convert the field to
>a "real" date field. This yields a blank (or maybe null) "realdate" :
>
>SELECT *, STR_TO_DATE(textdate,'%Y/%m/%d') as realdate FROM table ORDER BY
>realdate;[/color]
But the format you pass str_to_date doesn't match your examples!
You have to tell it what format to use. To take a really ridiculous
example:
01 02 03 04 05 06
now, which one is the year? the month?
Something like:
str_to_date(textdate, '%M %d, %Y %h:%i %p')
might work. I don't think it does timezones. Read the documentation for
date_format to get the meaning of all the %codes.
[color=blue]
>Can anyone suggest a way to fix this (aside from data entry) ? I'm willing to
>work with complex field manipulations -- or will quite happily ALTER the table
>to add a new realdate field to avoid future problems. But I'm pretty much at a
>loss about where to start.[/color]
If you can get the format correct so it works, and you're going to be doing
much comparing and sorting on the field, I suggest making a new realdate
field, loading it from the text field:
update table set realdate = str_to_date(textdate, '%M %d, %Y %h:%i %p');
modify your queries to use the new field (and, especially, enter a correct
date in realdate), and eventually drop the old one.
Gordon L. Burditt |