calculate difference in milli seconds

calculating date difference using datediff in milliseconds is limited (For milliseconds, the maximum number is 24 days, 20 hours, 31 minutes and 23.647 seconds.).
the constraint is its return type INT.

Is there any other way to find out the diff in millisencods(no manual calculation plz..)

thanks in adv
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

declare @d1 datetime, @d2 datetime
set @d1 = getdate()
set @d2 = dateadd(hour, 5, getdate()+25) -- 25 days and 5 hours later

select cast(datediff(day, @d1, @d2) as bigint) * 86400000 +
 datediff(millisecond, dateadd(day, datediff(day, @d1, @d2), @d1), @d2) as datediff_ms
You can make it a user-defined function as follows

create function ufn_datediff_ms(@d1 datetime, @d2 datetime)
returns bigint as
    return cast(datediff(day, @d1, @d2) as bigint) * 86400000 +
 datediff(millisecond, dateadd(day, datediff(day, @d1, @d2), @d1), @d2)

-- how to use it (don't omit the dbo/owner prefix)
select dbo.ufn_datediff_ms('2004.01.01', '2004.01.26') as diff_in_ms


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
The idea is to
- use bigint as return datatype
- calculate the difference in days and multiply by nb of millisecs in a day (1)
- calculate the diff in ms on the hours alone for the same day (reduce day offset) (2)
- add (1) and (2)
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

mcprakash4uAuthor Commented:
this is as good as

return datediff(ss, @d1,@d2)*1000

Bear in mind that the Datetime field in SQL only holds time to "accuracy of one three-hundredth of a second (equivalent to 3.33 milliseconds or 0.00333 seconds). " (from BOL)


>>this is as good as
return datediff(ss, @d1,@d2)*1000
Not exactly the same though
Yours rounds the results at the second
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.