Certainly, one has to be a little careful, viz:CREATE TABLE DateTest (id int PRIMARY KEY, dt smalldatetime not null)DECLARE @i int, @curdt smalldatetimeSET @i = 0SET @curdt = '2000-01-01'SET NOCOUNT ONWHILE @i < 2000BEGIN SET @curdt = DATEADD(s, RAND()*86400, @curdt) INSERT INTO DateTest VALUES (@i, @curdt) SET @i = @i + 1ENDSET NOCOUNT OFF
SET STATISTICS TIME ONSELECT COUNT(*)FROM DateTest D1CROSS JOIN DateTest D2WHERE CONVERT(varchar(10), D1.dt, 101) = CONVERT(varchar(10), D2.dt, 101)SELECT COUNT(*)FROM DateTest D1CROSS JOIN DateTest D2WHERE DATEADD(day, DATEDIFF(day, '1900-01-01', D1.dt), '1900-01-01') = DATEADD(day, DATEDIFF(day, '1900-01-01', D2.dt), '1900-01-01')-- but why bother with a DATEADD here?SELECT COUNT(*)FROM DateTest D1CROSS JOIN DateTest D2WHERE DATEDIFF(day, '1900-01-01', D1.dt) = DATEDIFF(day, '1900-01-01', D2.dt)SELECT COUNT(*)FROM DateTest D1CROSS JOIN DateTest D2WHERE DATEDIFF(day, D1.dt, D2.dt) = 0
Clearly the last one is slower, but that's because it has to perform the DATEDIFF on each pair as a loop join, rather than precalculate and then hash and probe. However, DATEADD and DATEDIFF themselves are significantly faster than CONVERT for this, as shown by the second query and third queries.Edited by - Arnold Fribble on 06/18/2002 09:26:58