Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the optimal way to compare dates in Microsoft SQL server?

I have a SQL datetime field in a very large table. It's indexed and needs to be queried.

The problem is that SQL always stores the time component (even though it's always midnight), but the searches are to the day, rather than time.

declare @dateVar datetime = '2013-03-11;  select t.[DateColumn] from MyTable t where t.[DateColumn] = dateVar; 

Won't return anything, as the t.[DateColumn] always includes a time component.

My question is what is the best way round this?

There seem to be two main groups of options:

  1. Create a second variable using dateadd and use a between ... and or >= ... and ... <=.

  2. Convert the t.[DateColumn] into a date-only component - I think this will cause any indexes to be ignored.

Both of these seem very messy - I don't really want to be making a range comparison or scan the table.

Is there a better way?

If one of these options is consistently optimal way then how and why?

like image 217
Keith Avatar asked Mar 11 '13 17:03

Keith


People also ask

How compare dates in MS SQL Server?

In SQL, the date value has DATE datatype which accepts date in 'yyyy-mm-dd' format. To compare two dates, we will declare two dates and compare them using the IF-ELSE statement. We can declare variables easily by using the keyword DECLARE before the variable name.

How do you compare dates in SQL?

We can compare dates using Comparison Operators in SQL like, = (Equals), < (Less than), > (Greater than), <= (Less than Equal), >= (Greater than Equal), <> (Not Equal), etc.

Can we compare date with string in SQL?

The methods used for date comparison varies across SQL database servers. But usually, there is a basic procedure for it. If the date is not already in the date format, then use data type conversion functions and convert it from string to DATE data type and then make the relevant comparison.

How do you check if one date is greater than another in SQL?

In this article, we will see the SQL query to check if DATE is greater than today's date by comparing date with today's date using the GETDATE() function. This function in SQL Server is used to return the present date and time of the database system in a 'YYYY-MM-DD hh:mm: ss. mmm' pattern.


1 Answers

Converting to a DATE or using an open-ended date range in any case will yield the best performance. FYI, convert to date using an index are the best performers. More testing a different techniques in article: What is the most efficient way to trim time from datetime? Posted by Aaron Bertrand

From that article:

DECLARE @dateVar datetime = '19700204';  -- Quickest when there is an index on t.[DateColumn],  -- because CONVERT can still use the index. SELECT t.[DateColumn] FROM MyTable t WHERE = CONVERT(DATE, t.[DateColumn]) = CONVERT(DATE, @dateVar);  -- Quicker when there is no index on t.[DateColumn] DECLARE @dateEnd datetime = DATEADD(DAY, 1, @dateVar); SELECT t.[DateColumn]  FROM MyTable t WHERE t.[DateColumn] >= @dateVar AND        t.[DateColumn] < @dateEnd; 

Also from that article: using BETWEEN, DATEDIFF or CONVERT(CHAR(8)... are all slower.

like image 161
Aleksandr Fedorenko Avatar answered Sep 22 '22 07:09

Aleksandr Fedorenko