Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

Below is my MySQL table,

Transations  ID    TimeStamp
aaa          11    8/1/2013 6:27:15 PM
bbb          11    8/1/2013 6:30:25 PM
ccc          11    8/1/2013 6:32:25 PM
ddd          11    8/1/2013 7:32:18 PM
eee          11    8/1/2013 8:34:32 PM
fff          11    8/1/2013 9:34:32 PM
ggg          11    8/1/2013 9:40:32 PM
hhh          11    8/1/2013 10:00:00 PM
iii          11    8/1/2013 10:11:02 PM
jjj          11    8/1/2013 10:15:04 PM
kkk          11    8/1/2013 10:21:05 PM
bbb          11    8/1/2013 10:27:09 PM


I am using below MySQL query to fetch data between two dates,
SQL
SELECT DISTINCT Transaction_Name FROM `availabilitydb`.`transaction` WHERE Id=11 AND TimeStamp Between '8/1/2013 6:27:25 PM' AND '8/1/2013 9:34:32 PM'


The above query works properly and it gives me below data,
aaa
bbb
ccc
ddd
eee
fff

But when I change the date value in the between condition and use '8/1/2013 6:27:25 PM' AND '8/1/2013 10:27:09 PM' in the query, it does not give any output,

SQL
SELECT DISTINCT Transaction_Name FROM `availabilitydb`.`transaction` WHERE Id=11 AND TimeStamp Between '8/1/2013 6:27:25 PM' AND '8/1/2013 10:27:09 PM'


Output=> Blank

I dont know why this is happening, the query is not able to fetch data between
'8/1/2013 6:27:25 PM' AND '8/1/2013 10:27:09 PM'

The TimeStamp used in the above table is not Date, it is Varchar.

I think that's why it is not able to fetch the data, but it is only happening for below condition,
'8/1/2013 6:27:25 PM' AND '8/1/2013 10:27:09 PM'

Could you please check and let me know what can be done.

Thanks in advance,
Kane
Posted
v2
Comments
ZurdoDev 2-Aug-13 9:18am    
1. If it is not a date, I would convert to date.
2. Just a preference and probably not the issue at all but I would suggest using 24hour times instead of AM PM.
Mike Meinz 2-Aug-13 10:19am    
In tour database, you need to change the Data Type of Timestamp to DateTime. It is a poor practice to store a date/time as a Varchar. It will continually cause issues for you like the one in your question. Also, when using date literals, it is better to use the ISO format date YYYY-MM-DD rather than MM/DD/YYYY or DD/MM/YYYY.
Maciej Los 5-Aug-13 1:56am    
Do not use VARCHAR to store dates. I would recommend you to change data type for this field. See my answer.
Maciej Los 5-Aug-13 3:56am    
Who delete my answer???

1 solution

First of all, please, read my comment. To convert string to date, use STR_TO_DATE[^] function.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900