Problem solve Get help with specific problems with your technologies, process and projects.

Calculating the difference in minutes between two datetime columns

I would like to calculate the difference in minutes between two datetime columns. Sometimes there will not be a second datetime for comparison, so I need to handle that. I am currently performing a simple subtraction and storing the result as datetime, which is accurate but yields the result as time since 1900.

Use the DATEDIFF function e.g.

 select datediff(mi,Date1, IsNull(Date2, Date1)) from Table1
Here we also use the IsNull function to say give us the difference in minutes between Date1 and Date2 unless Date2 is Null in which case use Date1 (which will produce a difference of 0).


For More Information

This was last published in November 2002

Dig Deeper on Microsoft SQL Server Installation

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.