Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to use datetime difference in SQL Select?
Message
 
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Environment versions
SQL Server:
SQL Server 2005
Miscellaneous
Thread ID:
01390159
Message ID:
01390314
Views:
36
>>
>>SELECT SUM(CAST(DATETIME1 - DATETIME2 AS int))  AS NUMDAYS
>>
>>>
>
>Thank you, Sergey. I wonder if anyone ever tested what is faster, DATEDIFF() or CAST().

DATEDIFF would be faster.

Try Boris's sample:
DECLARE @Test TABLE (Fld1 datetime, Fld2 datetime)
DECLARE @i int
SET @i = 0
WHILE @i < 1000
      BEGIN 
         INSERT INTO @Test VALUES (GETDATE()+@i, GETDATE()+@i*20)
         SET @i = @i +1
      END
set statistics time on  
    
SELECT SUM(cast(Fld2-Fld1 as int)) FROM @Test
SELECT SUM(DATEDIFF(dd,Fld1,Fld2)) FROM @Test
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform