syntax101 Posted November 19, 2008 Share Posted November 19, 2008 i have a table and my cc_date(varchar type) and the date format is 1/31/2008, 2/28/2008 im having trouble selecting them by range. my query is select * from cc where cc_date >= ? can anybody help me how to query that varchar type to date(format) pls help im using mysql 5.0 Quote Link to comment https://forums.phpfreaks.com/topic/133349-help-with-mysql-query-varchar-to-date/ Share on other sites More sharing options...
Mchl Posted November 19, 2008 Share Posted November 19, 2008 First of all, don't store dates as varchar. This is just one of the problems you will experience if you do. The DATE, TIME, DATETIME, TIMESTAMP field types were introduced for a reason. Second: not all is lost. You can use STR_TO_DATE() mysql function. Quote Link to comment https://forums.phpfreaks.com/topic/133349-help-with-mysql-query-varchar-to-date/#findComment-693518 Share on other sites More sharing options...
syntax101 Posted November 19, 2008 Author Share Posted November 19, 2008 yeah im having problems now. can u give me a sample query based on my sample format i tried that but i cannot get it working. please. i will learn my mistake. :'( Quote Link to comment https://forums.phpfreaks.com/topic/133349-help-with-mysql-query-varchar-to-date/#findComment-693520 Share on other sites More sharing options...
syntax101 Posted November 19, 2008 Author Share Posted November 19, 2008 here is my query SELECT cc_num, cc_jobnumber, emp_name, cc_entry, cc_cc FROM cc WHERE STR_TO_DATE('cc_date', '%m/%d/%Y') >= ? (1/1/2008) cc_date contains 1/1/2007 1/1/2008 1/2/2007 8/7/2008 1/31/2007 result must be 1/1/2008 8/7/2008 can anybody tell me whats wrong. i cannot display the result above Quote Link to comment https://forums.phpfreaks.com/topic/133349-help-with-mysql-query-varchar-to-date/#findComment-693545 Share on other sites More sharing options...
Mchl Posted November 19, 2008 Share Posted November 19, 2008 select * from cc where STR_TO_DATE(cc_date,'%m/%d/%Y') >= //here input date in mysql format (for example YYYY-MM-DD) Quote Link to comment https://forums.phpfreaks.com/topic/133349-help-with-mysql-query-varchar-to-date/#findComment-693548 Share on other sites More sharing options...
syntax101 Posted November 19, 2008 Author Share Posted November 19, 2008 SELECT cc_num, cc_jobnumber, emp_name, cc_entry, cc_cc, cc_date FROM cc WHERE STR_TO_DATE(cc_date, '%m/%d/%Y') >= STR_TO_DATE(?, '%m/%d/%Y') here the final query and it works thank you Mchl Quote Link to comment https://forums.phpfreaks.com/topic/133349-help-with-mysql-query-varchar-to-date/#findComment-693550 Share on other sites More sharing options...
Mchl Posted November 19, 2008 Share Posted November 19, 2008 Try to convert your dates to proper format. It should not be difficult, and you'll save yourself a lot of problems,time and storage space in the future. Quote Link to comment https://forums.phpfreaks.com/topic/133349-help-with-mysql-query-varchar-to-date/#findComment-693553 Share on other sites More sharing options...
syntax101 Posted November 19, 2008 Author Share Posted November 19, 2008 yes MChl thanx again. Quote Link to comment https://forums.phpfreaks.com/topic/133349-help-with-mysql-query-varchar-to-date/#findComment-693555 Share on other sites More sharing options...
diasansley Posted March 8, 2011 Share Posted March 8, 2011 thanks works fab Quote Link to comment https://forums.phpfreaks.com/topic/133349-help-with-mysql-query-varchar-to-date/#findComment-1184328 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.