I have a field called Timestamp, that stores its values as text as opposed to an actual Timestamp. The logging application is unchangeable, unfortunately.
So
table.Timestamp -> text field with format -> "Wed Mar 02 13:28:59 CDT 2011"
I have been developing a query to purge all but the most recent row using this as my Timestamp selector, which is also converting the string into a date ->
MAX( STR_To_DATE( table.Timestamp , '%a %b %d %H:%i:%s CDT %Y' )
My query works perfectly…
However, what I’ve found is that the string value -> ‘CDT’ -> changes between ‘CDT’ and ‘CST’ depending on whether the current time is daylight savings time or not. During daylight savings time, it logs as ‘CDT’, and vice versa.
So all the rows that contain ‘CST’ get ignored when I run this ->
MAX( STR_To_DATE( table.Timestamp , '%a %b %d %H:%i:%s CDT %Y' )
and all the rows that contain ‘CDT’ get ignored when I run this ->
MAX( STR_To_DATE( table.Timestamp , '%a %b %d %H:%i:%s CST %Y' )
Is there a way to make it run against both string formats?
EDIT –
So given the following data (different only by ‘CDT and ‘CST’:
"Wed Mar 02 13:28:59 CDT 2011" and "Tue Mar 08 09:42:07 CST 2011"
Currently it will return as follows:
MAX( STR_To_DATE( table.Timestamp , '%a %b %d %H:%i:%s CDT %Y' ) Returns - Wed Mar 02 13:28:59 CDT 2011
MAX( STR_To_DATE( table.Timestamp , '%a %b %d %H:%i:%s CST %Y' ) Returns - Tue Mar 08 09:42:07 CST 2011
I want it to return “Tue Mar 08 09:42:07 CST 2011” no matter what.
EDIT 2:
This is my main query:
DELETE
FROM `table` main
WHERE STR_To_DATE( main.Timestamp , '%a %b %d %H:%i:%s CST %Y' ) <
(SELECT COALESCE(STR_To_DATE( sub.Timestamp , '%a %b %d %H:%i:%s CDT %Y' ), STR_To_DATE( sub.Timestamp , '%a %b %d %H:%i:%s CST %Y' ))
FROM `table` sub
WHERE sub.Retrieving_User = main.Retrieving_User )
Coalesce the two.
COALESCEmeans to return the first non-null value in the set.In this case it will first try to parse it using “CDT”. If that fails, returning NULL, it will try to parse using “CST” instead. (If that fails, it will reach the end of the coalesce list and just return NULL, but in your case it won’t get that far).
SQL Fiddle Try changing the data in the table and see it work.