erme Posted February 23, 2010 Share Posted February 23, 2010 Hi I am trying to order by date so that the results are ordered by what's closet. This is what I have but doesn't work: SELECT * FROM $Table ORDER BY cast(Expiry as DateTime) DESC 'Expiry' being a date format such as 10/02/2010 Link to comment https://forums.phpfreaks.com/topic/193084-order-by-castas-datetime/ Share on other sites More sharing options...
mapleleaf Posted February 23, 2010 Share Posted February 23, 2010 Can you not do ORDER BY Expiry? Why cast? Link to comment https://forums.phpfreaks.com/topic/193084-order-by-castas-datetime/#findComment-1016888 Share on other sites More sharing options...
erme Posted February 23, 2010 Author Share Posted February 23, 2010 Doesn't seem to work as a date. Because the fields are in the format 12/12/02 they don't get orders correctly. Link to comment https://forums.phpfreaks.com/topic/193084-order-by-castas-datetime/#findComment-1016899 Share on other sites More sharing options...
fenway Posted February 24, 2010 Share Posted February 24, 2010 Then use STR_TO_DATE() to fix that up first. Link to comment https://forums.phpfreaks.com/topic/193084-order-by-castas-datetime/#findComment-1017642 Share on other sites More sharing options...
erme Posted February 25, 2010 Author Share Posted February 25, 2010 How does STR_TO_DATE() work? Link to comment https://forums.phpfreaks.com/topic/193084-order-by-castas-datetime/#findComment-1018094 Share on other sites More sharing options...
fenway Posted February 25, 2010 Share Posted February 25, 2010 How does STR_TO_DATE() work? The refman is your friend! Link to comment https://forums.phpfreaks.com/topic/193084-order-by-castas-datetime/#findComment-1018261 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.