Click here to Skip to main content
15,888,162 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
dateTime startTime ;
dateTime endTime;

i want search to database between startTime & endTime

like : startTime < timeField < endTime

how to do to this ?
Posted

Hi

For do that you have more solutions for example you can use BETWEEN operator same as:
SQL
SELECT * FROM MyTable WHERE Time BETWEEN startTime AND endTime 


I suggest to you go to the below link for getting more information:

http://www.w3schools.com/SQl/default.asp[^]
http://www.w3schools.com/SQl/sql_between.asp[^]

Best Regards.
 
Share this answer
 
v7
try this.
SQL
SELECT * FROM TABLE WHERE TimeField BETWEEN startTime AND endTime
 
Share this answer
 
v2
If you have exact data and time in those startTime and endTime, then go for BETWEEN.
For example -
SQL
WHERE myDateTime 
      BETWEEN 
          '2008-01-01 00:00:00'
      AND 
          '2008-03-31 23:59:59.993'


Otherwise you should compare by Greater Than and Less Than operators.
Else you will miss many rows when it will compare it to DateTime Field.

For example -
SQL
WHERE myDateTime 
      BETWEEN 
          '2008-01-01'
      AND 
          '2008-03-31'


Here it won't select the date '2008-03-31 09:37', but it falls in the range.
Reason is, it converts the query to below.
SQL
WHERE myDateTime 
      BETWEEN 
          '2008-01-01 00:00:00'
      AND 
          '2008-03-31 00:00:00'


So, a much better approach is to abandon the BETWEEN condition, and do this instead:
SQL
WHERE myDateTime >= '2008-01-01'
      AND 
      myDateTime  < '2008-04-01'


Reference - Using BETWEEN with DATETIMEs in SQL[^]
 
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