jaikob Posted August 22, 2008 Share Posted August 22, 2008 okay, I have a varchar field in mysql, in order to store dates in the form of: 1/1/08. No leading zeros in the single digits. To sort the varchar field by date, Im trying to use STR_TO_DATE function, but It will not work. Here is my sql: SELECT *, STR_TO_DATE(date, '%m/%d/%y') AS date_sort FROM publications WHERE type = 'News Letter' AND archive = 'Active' ORDER BY date_sort DESC I get 00-00-000 Down the date row when I run the query. How do I get this to work? Link to comment https://forums.phpfreaks.com/topic/120790-solved-help-with-date-sorting/ Share on other sites More sharing options...
AjBaz100 Posted August 22, 2008 Share Posted August 22, 2008 Not sure but there could be a problem with your implementation of STR_TO_DATE If you do a select without the STR_TO_DATE function, what is the result set. i.e SELECT date FROM publications WHERE type = 'News Letter' AND archive = 'Active' ORDER BY date DESC Link to comment https://forums.phpfreaks.com/topic/120790-solved-help-with-date-sorting/#findComment-622673 Share on other sites More sharing options...
jaikob Posted August 22, 2008 Author Share Posted August 22, 2008 Update, I have changed all of my dates to MM/DD/YYYY format. But STR_TO_DATE still sorts by the DD first, then the YYYY Part. So it looks like this: 01/26/2006 02/26/2006 02/27/2007 02/28/2007/ 03/01/2006 Link to comment https://forums.phpfreaks.com/topic/120790-solved-help-with-date-sorting/#findComment-622675 Share on other sites More sharing options...
jaikob Posted August 22, 2008 Author Share Posted August 22, 2008 Not sure but there could be a problem with your implementation of STR_TO_DATE If you do a select without the STR_TO_DATE function, what is the result set. i.e SELECT date FROM publications WHERE type = 'News Letter' AND archive = 'Active' ORDER BY date DESC it sorts it by the first few #'s. Not the actual year. It is a varchar field by the way. Link to comment https://forums.phpfreaks.com/topic/120790-solved-help-with-date-sorting/#findComment-622676 Share on other sites More sharing options...
jaikob Posted August 22, 2008 Author Share Posted August 22, 2008 Is there anyway I can update the whole table using STR_TO_DATE? for example: UPDATE publications, STR_TO_DATE('date', '%m/%d/%Y') AS date_sort SET date=date_sort that doesn't work though. Link to comment https://forums.phpfreaks.com/topic/120790-solved-help-with-date-sorting/#findComment-622689 Share on other sites More sharing options...
AjBaz100 Posted August 22, 2008 Share Posted August 22, 2008 execute this: SELECT STR_TO_DATE('04/31/2004', '%m/%d/%Y'); Does it return a valid date.? Link to comment https://forums.phpfreaks.com/topic/120790-solved-help-with-date-sorting/#findComment-622694 Share on other sites More sharing options...
jaikob Posted August 22, 2008 Author Share Posted August 22, 2008 yes. Link to comment https://forums.phpfreaks.com/topic/120790-solved-help-with-date-sorting/#findComment-622696 Share on other sites More sharing options...
jaikob Posted August 22, 2008 Author Share Posted August 22, 2008 I fixed it myself. Note to Self: Do not use empty fields with mysql functions. Thanks! Link to comment https://forums.phpfreaks.com/topic/120790-solved-help-with-date-sorting/#findComment-622702 Share on other sites More sharing options...
fenway Posted August 22, 2008 Share Posted August 22, 2008 I fixed it myself. Note to Self: Do not use empty fields with mysql functions. Thanks! I really hope you didn't change this field from a DATE column. Link to comment https://forums.phpfreaks.com/topic/120790-solved-help-with-date-sorting/#findComment-623190 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.