The ODBC format uses two digits for month and date, ie 2015-08-29 00:00:00
instead of 2015-8-29 00:00:00
.
It would be better though to use date-typed parameters. Otherwise, unambiguous date literals should be used, that don't require any conversions and aren't affected by the collation.
The full ISO 8601 format doesn't require conversions, eg: 2015-08-29T00:00:00
. The unseparated date format also doesn't require conversion, eg 20150829
.
If UPLOAD_DATE
is a date
column, there is no reason to specify the time part. The query could be written as follows:
declare @startDate date, @endDate date
....
Select *
From BILLS
Where UPLOAD_DATE BETWEEN @startDate AND @endDate
If the statement is part of a stored procedure, or is called by ADO.NET, this is the preferred way, eg:
var startDate=new DateTime(2015,8,29);
var endDate=startDate.AddMonths(1).AddDays(-1);
var myCmd=new SqlCommand("SELECT .... Where UPLOAD_DATE BETWEEN @startDate AND @endDate");
myCmd.Parameters.Add("@startDate",startDate);
myCmd.Parameters.Add("@endDate",endDate);
....
Using date literals the same statement can be:
Select *
From BILLS
Where UPLOAD_DATE BETWEEN '20150829' AND '20150928'
The time part would be required only if UPLOAD_DATE
is a datetime
type. In this case though, it's easier to change the criteria to be less than the next date:
Select *
From BILLS
Where UPLOAD_DATE >= '20150829' AND UPLOAD_DATE < '20150929'