I am trying to make one SELECT
that brings me all the records that are between two dates. However, I want to ignore the time (hours, minutes, seconds, etc...). For example, I have a record that has 2016-11-14 11:47:56.207
, but I send by parameter to my stored procedure
only 14-11-2016
. If I send it like this, I get no result.
WHERE GRP.GRP_Fecha BETWEEN @desde AND @hasta
In your case, I suggest you avoid any solution that involves doing one
convert
type of conversion or another with the fieldGRP_Fecha
. By doing so, you do not allow SQL Server to be able to use an index if there is one for the fieldGRP_Fecha
and this can affect your performance in very obvious ways.And of course
between
it's not ideal in this specific case for the reasons already mentioned in your question.I suggest the following condition for the best performance (good use of the indexes) and to avoid problems with the hours:
In the case where:
@desde
=2016-11-01
@hasta
=2016-11-14
... the condition is equal to:
When asking for dates before
2016-11-15
, this includes all dates of2016-11-14
regardless of time.I solved the problem with the
convert
. For your case it would beThis will work for you even if it has a Year/month/day format: Hour:Min:Seg