Alicia Posted November 15, 2012 Share Posted November 15, 2012 Hi guys, can somebody give me an idea why this query doesn't work? thanks SELECT SUM(credit) AS usage, STR_TO_DATE(`created`, '%m/%d/%y'),STR_TO_DATE(`expiry`, '%m/%d/%y') FROM `suax` WHERE STR_TO_DATE(`created`, '%m/%d/%y') >= '11/5/2012' AND STR_TO_DATE(`expiry`, '%m/%d/%y') = '11/15/2012' LIMIT 0 , 30 my created and expiry format is a string like this 1/15/2012 04:05:22 Please advise.thanks Link to comment https://forums.phpfreaks.com/topic/270710-str-to-date-not-working/ Share on other sites More sharing options...
Pikachu2000 Posted November 15, 2012 Share Posted November 15, 2012 In what way does it not work, pray tell? Link to comment https://forums.phpfreaks.com/topic/270710-str-to-date-not-working/#findComment-1392515 Share on other sites More sharing options...
Alicia Posted November 15, 2012 Author Share Posted November 15, 2012 i just want to filter the records by referrring to the date i have in the column but currently it is in varchar so i couldn't filter by date range Link to comment https://forums.phpfreaks.com/topic/270710-str-to-date-not-working/#findComment-1392517 Share on other sites More sharing options...
Pikachu2000 Posted November 15, 2012 Share Posted November 15, 2012 Again, in what way is it not working? Link to comment https://forums.phpfreaks.com/topic/270710-str-to-date-not-working/#findComment-1392518 Share on other sites More sharing options...
Alicia Posted November 15, 2012 Author Share Posted November 15, 2012 i am not sure... there is a syntax error when i run the query Link to comment https://forums.phpfreaks.com/topic/270710-str-to-date-not-working/#findComment-1392519 Share on other sites More sharing options...
Jessica Posted November 15, 2012 Share Posted November 15, 2012 One problem I see is that 1/15/2012 04:05:22 is NOT '%m/%d/%y'. 1/15/2012 would be %m/%d/%y. I've NEVER used that function so I don't know if that it is. However, you got a syntax error, and you didn't post it...why exactly? Link to comment https://forums.phpfreaks.com/topic/270710-str-to-date-not-working/#findComment-1392521 Share on other sites More sharing options...
Barand Posted November 15, 2012 Share Posted November 15, 2012 So many errors! You are not correctly defining the current format to be converted (see Jessica's reply) You are converting to mysql date format yet comparing against your string format (use >= 'yyyy-mm-dd' etc) You logic is screwed - you look for those greater than one date AND equal to another (only ever returning the latter) You need column aliases for your converted dates EDIT: The time element will be ignored mysql> select STR_TO_DATE('22/01/2012 09:30:00','%d/%m/%Y') as dob; +------------+ | dob | +------------+ | 2012-01-22 | +------------+ Link to comment https://forums.phpfreaks.com/topic/270710-str-to-date-not-working/#findComment-1392587 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.