I am trying to retrieve the data between two dates using C# with SQL Server database. I am passing dates entered in two text boxes to the sql statement as show below
"SELECT SUM(CONVERT(DECIMAL(6,2),howmuchspent)) AS totalexpenses FROM MONTHLYEXPENSES WHERE expensesdate BETWEEN ' " + fromdate.Text + " ' AND '" + todate.Text + "' ";
With the statement above I am getting data that from the required date range and also data that is out of date range too.
When I tested the statement in SQL Server query screen by giving dates as follows
SELECT SUM(CONVERT(DECIMAL(6,2),howmuchspent)) AS totalexpenses FROM MONTHLYEXPENSES WHERE expensesdate BETWEEN ' 03/01/2010 ' AND '03/13/2010'
I get data exactly between those dates.
What was the wrong I was doing in retrieving data programatically?
Any help is appreciated. Thank you
Loading
theLizardPosted Mar 16, 2010, 1:23 AM
SELECT sum(spent)as total FROM tbl where dte >= '2010/01/01' AND dte <= '2010/03/01'
SELECT sum(spent)as total FROM tbl where dte between '2010/01/01' AND '2010/03/01'
it returned the values 63.50 which is correct.
if you just want the values between but not including the from date and to date then do this
SELECT sum(spent)as total FROM tbl where dte > '2010/01/01' AND dte < '2010/03/08'
this will give $40.00 as the result.
Prasad VempatiPosted Mar 16, 2010, 12:35 AM
I did entered 3 records with 3 dates 2010/12/03, 2010/10/03 and 2010/25/02. When I tried to retrieve with fromdate=2010/01/02 and todate=2010/28/02, I still received all three dates.
Thank you
Prasad
theLizardPosted Mar 14, 2010, 10:51 PM
I don't think that using C# has anything to do with your problem.
Sam HobbsPosted Mar 14, 2010, 6:18 PM
Prasad VempatiPosted Mar 14, 2010, 5:27 PM
Thanx for your reply. I did look the data with Response.write statement. It is exactly giving the date as
fromdate.text as 03/01/2010 and todate.text as 03/13/2010
Thanx again
Sam HobbsPosted Mar 14, 2010, 5:24 AM