Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Understanding DATEDIFF()
Message
From
16/04/2015 21:32:19
 
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Environment versions
SQL Server:
SQL Server 2012
Application:
Web
Miscellaneous
Thread ID:
01618557
Message ID:
01618578
Views:
33
>The problem here is indirect casting to float and therefore wrong calculations. Try casting to DECIMAL to obtain correct result, e.g.
>
>
>;with cteDates as (select dateadd(day, n.number, '20000101') as [startDate],
>dateadd(day, n.number, '20000102') as [endDate]
>from dbo.numbers N where N.number  between 0 and 364)
>
>select SUM(datediff(day, startDate, endDate)) as [Days Count],
>SUM(cast(datediff(second, startDate, endDate)  as decimal(12,2))/86400)  as [Days /Seconds Count],
>SUM(datediff(second, startDate, endDate))/86400.00  as [Wrong Days /Seconds Count]
>from cteDates 
>
Thanks
Michel Fournier
Level Extreme Inc.
Designer, architect, owner of the Level Extreme Platform
Subscribe to the site at https://www.levelextreme.com/Home/DataEntry?Activator=55&NoStore=303
Subscription benefits https://www.levelextreme.com/Home/ViewPage?Activator=7&ID=52
Previous
Reply
Map
View

Click here to load this message in the networking platform