Question : How to write a SQL select query to search by date

I need to write a query that will limit my select statement to a date range.  I have captured the dates to use from the user.  I am using VB script in an ASP page connecting to MS SQL (sqlexpress 2005 server).

Here is the select statement that currently results in all records (regardless of date).

SELECT * FROM TBLReadings WHERE AccountNumber = '" & acct & "' AND DeviceId = '0' AND DeviceNo = '" & device & "' ORDER BY DateReceived DESC

I need to add a WHERE into this statement.  I want to use "startdate" and "enddate" so that the statement will return results that are equal or in the range of the startdate and enddate.  The date field in the SQL DB is called "DateReceived"

The "startdate" and "enddate" will be VB variables to use similarly to the "acct" and "device" variables above.

DateReceived is stored in the DB as a date type.  The date format I have captured in the start and end date variables are stored as MM/DD/YY.

Answer : How to write a SQL select query to search by date

here is the query
1:
2:
3:
4:
5:
SELECT * 
  FROM TBLReadings 
 WHERE AccountNumber = '" & acct & "' AND DeviceId = '0' AND DeviceNo = '" & device & "' 
   and DateReceived between convert(datetime, '" & startdate & "',1) and convert(datetime, '" & enddate & "',1)
 ORDER BY DateReceived DESC
Random Solutions  
 
programming4us programming4us