In SQL Server 2008R2 fetching data between two dates is not working.
I am using the following code to get rows from the INVOICE table where the CREATED_DATE is between @startdate and @enddate, which are the parameters I am sending to the stored procedure.
Select *
from INVOICES INV
where CONVERT(Varchar(10), INV.CREATED_DATE, 105)
BETWEEN CONVERT(VARCHAR(10), @startdate, 105)
AND CONVERT(VARCHAR(10), @enddate, 105)
It is not working properly, driving me nuts..
What I am doing wrong?..
Since you’re on SQL Server 2008 R2, you could just convert everything to the
DATEformat:Since you’re using the
DATEtype, you’re independent of any dateformat or language settings or any of those tricky features. SQL Server will just compare dates – as it should.And of course: if you make your stored procedure parameters
@startdateand@enddateto be of typeDATEfrom the beginning, then you can save yourself those twoCASToperations, too!(and if you made
CREATED_DATEof typeDATE– you could even forget about that lastCASTin the statement, too!)