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:
01390313
Views:
48
It took me a while to figure out why you were laughing.

>>>Hi,
>>>
>>>How do you SUM number of days difference between two DateTime type fields as following:
>>>
>>>
>>>select SUM( DATETIME1 - DATETIME2) AS NUMDAYS
>>>
>>
>>It should work the way it's
>
>I got:
>Operand data type datetime is invalid for sum operator.
>Because DATETIME1 - DATETIME2 is also datetime.
>
>DECLARE @Test TABLE (Fld1 datetime, Fld2 datetime)
>DECLARE @i int
>SET @i = 0
>WHILE @i < 20
>      BEGIN 
>         INSERT INTO @Test VALUES (GETDATE()+@i, GETDATE()+@i*20)
>         SET @i = @i +1
>      END
>SELECT SUM(Fld2-Fld2) FROM @Test
>
If it's not broken, fix it until it is.


My Blog
Previous
Reply
Map
View

Click here to load this message in the networking platform