Click here to Skip to main content
15,908,015 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have table in there is a coloumn name date. I just want to fetch the data from table between 1 a.m and 5 a.m (both inclusive) not considering the date.

i am doing like this

select count(date) as number_of_queries
from query
where date

DATE

2012-01-05 01:00:00.0

2013-06-01 09:00:00.0

2014-01-02 04:00:00.0

2013-09-10 13:00:00.0

2012-01-05 14:00:00.0

2013-06-01 19:00:00.0

2014-01-02 14:00:00.0

2014-09-10 13:00:00.0
Posted
Updated 10-Apr-15 0:10am
v2
Comments
Maciej Los 10-Apr-15 5:53am    
Please, post sample data. Use "Improve widget". Do not forget to properly format text.
Harshit Wadhera 10-Apr-15 6:03am    
i will explain you there is only 1 table in which there are 5 rows and i only had to fetch the data from that table. It only consists 3 rows which are between that time.
The problem is that date can be any date.
Maciej Los 10-Apr-15 6:07am    
Thank you for clarification. I'll be glad if you'll be so kind and provide sample data.
Harshit Wadhera 10-Apr-15 6:10am    
i had entered the data
Harshit Wadhera 10-Apr-15 6:10am    
only 2 data should be fetch 1 and 3

Try something like this:
SQL
SELECT DateField
FROM TableName
WHERE TIME(DateField) BETWEEN '01:00:00' AND '05:00:00'


For further information, please see:
https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html[^]
https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_time[^]
 
Share this answer
 
Try:
SQL
SELECT * FROM MyTable
WHERE CAST([date] as TIME) BETWEEN '01:00:00' AND '05:00:00'


Didn't notice it was MySQl:
SQL
SELECT * FROM MyTable
WHERE TIME([date]) BETWEEN '01:00:00' AND '05:00:00'
 
Share this answer
 
v2
Comments
Harshit Wadhera 10-Apr-15 6:41am    
I am getting error
check the manual that corresponds to your MySQL server version for the right syntax to use near '[date] as time) between '01:00:00' AND '05:00:00'' at line 1

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