Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends ,
I Want to access Email from my DateBase whose RecieveTime Is Between given dates ..
I only want to match or compare the date part ..
I write this query but there is some problem in time
I dont want to compare time.
I am Using Sql Server 2005 and .net 2.0 Frame work
SQL
select * from OUTLOOK_DETAIL where  convert(datetime,'" + Program.datefrom + "')<=RECEIVE_TIME  and  convert(datetime,'" + Program.dateto + "')>=RECEIVE_TIME order by RECEIVE_TIME
Posted
Updated 5-Sep-11 23:35pm
v3
Comments
Corporal Agarn 6-Sep-11 11:09am    
Watch the dateto as it could return 12 am as a time. Thus if your dateto = '09/06/2011' but your Receive_Time is '09/06/2011 05:00' it will not be returned.

Use the BETWEEN operator in sql.

http://msdn.microsoft.com/en-us/library/ms187922.aspx[^]
 
Share this answer
 
Comments
Reiss 6-Sep-11 5:57am    
Have 5 - btw, do you have this stock answer saved anywhere - you can almost guarantee that you will need at once a week ;)
Few things:
- use bind variables, see SqlParameter[^]
- use between operator

SQL
select * 
from OUTLOOK_DETAIL 
where RECEIVE_TIME BETWEEN @datefrom AND @dateto
order by RECEIVE_TIME
 
Share this answer
 
assuming that Program.datefrom & Program.dateto is in "mm/dd/yyyy" format, one option is as follows:

SQL
select * from
OUTLOOK_DETAIL
where  '" + Program.datefrom + "' <=  convert(varchar(10),RECEIVE_TIME,101) and  '" + Program.dateto + "' >= convert(varchar(10),RECEIVE_TIME,101) order by
RECEIVE_TIME


check the following link for other options:

http://weblogs.sqlteam.com/jeffs/archive/2004/12/02/2954.aspx[^]
 
Share this answer
 
Use this

SQL
SELECT * from OUTLOOK_DETAIL where BETWEEN convert(VARCHAR(20),'" + Program.datefrom + "',112) and convert(VARCHAR(20),'" + Program.dateto + "',112)
 
Share this answer
 
I agree with Mika that using parameters in your query is the way to go, but if you have to put the dates in as inline text, render them as ISO standard datetimes so that SQL will recognize them correctly:

SQL
select * from OUTLOOK_DETAIL
where RECEIVE_TIME between
            '" + Program.datefrom.ToString("yyyy-MM-dd HH:mm:ss") + "'
        and
            '" + Program.dateto.ToString("yyyy-MM-dd HH:mm:ss") + "'
order by RECEIVE_TIME


or if it only the date part:

SQL
select * from OUTLOOK_DETAIL
where RECEIVE_TIME between
            '" + Program.datefrom.ToString("yyyy-MM-dd") + "'
        and
            '" + Program.dateto.ToString("yyyy-MM-dd") + "'
order by RECEIVE_TIME
 
Share this answer
 
v2
elect * from OUTLOOK_DETAIL where convert(date,'" + Program.datefrom + "')<=RECEIVE_TIME and convert(date,'" + Program.dateto + "')>=convert(date,RECEIVE_TIME) order by RECEIVE_TIME

if you want to compare compare only date then convert into Date if you want only time then convrt both column into TIME ..

VIVEK
 
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