mdemetri2 Posted October 23, 2013 Share Posted October 23, 2013 (edited) Hello there Hoping someone might be able to steer me in the right direction here. I have a varchar field that is used to record a date stored in format Y-m-d H:i:s (e.g. 2013-08-07 09:20:46) I would like to run a script that means I can query this field, and allow me to do a between date a and b. Is this possible due to it being stored as a varchar and in such a format? Many thanks mdemetri2 Edited October 23, 2013 by mdemetri2 Quote Link to comment https://forums.phpfreaks.com/topic/283208-query-using-date-stored-as-varchar/ Share on other sites More sharing options...
gristoi Posted October 23, 2013 Share Posted October 23, 2013 why can you not amend the field type to a dateTime? Quote Link to comment https://forums.phpfreaks.com/topic/283208-query-using-date-stored-as-varchar/#findComment-1455080 Share on other sites More sharing options...
mdemetri2 Posted October 23, 2013 Author Share Posted October 23, 2013 thanks for the reply gristoi, I am unable to change the field type at this time but if that is the only way then I suppose I will have to at some stage. I thought there might be a way round it though. Quote Link to comment https://forums.phpfreaks.com/topic/283208-query-using-date-stored-as-varchar/#findComment-1455088 Share on other sites More sharing options...
Solution Barand Posted October 23, 2013 Solution Share Posted October 23, 2013 Make sure the date range limits are in the format 'yyyy-mm-dd'. To compare just the date portion of the date/time use DATE() function. eg $a = '2013-10-20'; $b = '2013-10-23' $sql = "SELECT something FROM mytable WHERE DATE(mydate) BETWEEN '$a' AND '$b' "; Quote Link to comment https://forums.phpfreaks.com/topic/283208-query-using-date-stored-as-varchar/#findComment-1455093 Share on other sites More sharing options...
mdemetri2 Posted October 23, 2013 Author Share Posted October 23, 2013 Barand, as usual quick response, I will certainly give that a try,.....will let you know how things go. thanks! Quote Link to comment https://forums.phpfreaks.com/topic/283208-query-using-date-stored-as-varchar/#findComment-1455148 Share on other sites More sharing options...
mdemetri2 Posted October 24, 2013 Author Share Posted October 24, 2013 Barand - thank you, that worked perfectly for what I required. Appreciated as always. thanks! Quote Link to comment https://forums.phpfreaks.com/topic/283208-query-using-date-stored-as-varchar/#findComment-1455182 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.