Friday, February 17, 2012

Cost of Comparing Dates, ignoring Time part

Hi, i would like to know what is better when trying to compare two data fields one of them has the format yyyymmdd and then the another is yyyymmdd hh:mmTongue Tieds

I need to ignore the time part but when I use this ...

where Convert(CHAR(8),Year(date)*10000+ Month(date)*100+Day(date))
between @.date_ini and @.date_fin


I have also tried this....

where convert(varchar(12), date, 112 )
between @.date_ini and @.date_fin

@.date_ini and @.date_fin are varchar '20070301'

The proble is that the time to execute the query is exagerated Sad

would you pleas help me?

Try:

where date >= @.date_ini
and date < convert(datetime, @.date_fin) + 1

The performance of your query should be improved if you have an index on your "date" field. Also, I have assumed that you have stoerd your field as an SQL datetime field and made the assumption that when you say that you wat @.date_ini and @.date_fin to be the same date that you are wanting all records on that date to be selected.

I do not use the BETWEEN operator because BETWEEN would also select data that occurs exactly on midnight of the next day.

|||

Thank you Kent Waldrop

It works better

No comments:

Post a Comment