VB.Net - SQL Qry DATEDIFF With Minutes and Seconds

Good Day Experts!

I am using the DATEDIFF in a qry to get the seconds between a Start and End time.  I would like to have the seconds in minutes and seconds if applicable.  

For example, I am getting 81 seconds on a record.  How do I get it to return 1:21?

Here is the line in my qry:


Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Jim HornMicrosoft SQL Server Data DudeCommented:
Something like this?

Declare @dt_start datetime = '2015-05-18 10:00:05', @dt_end datetime = '2015-05-18 10:02:14'
SELECT CAST(DATEADD(second, DATEDIFF(second, @dt_start, @dt_end), '19000101') as time) 

Open in new window

btw the above converts to a time, essentially '1900-01-01' plus the time difference in seconds, displaying only the time part.  This enables you to sum these times for a grand total if that is your needs.

If that's not your needs, you can always display it as a varchar by doing the DATEDIFF, divide by 60 and that's the minutes, add a :, then divide and take the modulo for the seconds.

Declare @dt_start datetime = '2015-05-18 10:00:05', @dt_end datetime = '2015-05-18 10:02:14', @seconds int

SET @seconds = DATEDIFF(second, @dt_start, @dt_end)
SELECT RIGHT('00' + CAST(@seconds / 60 as varchar(10)),2)  + ':' + RIGHT('00' + CAST(@seconds % 60 as varchar(10)),2)

Open in new window

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
Brian CroweDatabase AdministratorCommented:
DECLARE @StartTime      DATETIME = '5/18/2015 10:16',
      @EndTime            DATETIME = '5/18/2015 10:17:21';
SELECT @StartTime, @EndTime,
      CAST(DATEDIFF(SECOND, @StartTime, @EndTime) / 60 AS VARCHAR) + ':' + CAST(DATEDIFF(SECOND, @StartTime, @EndTime) % 60 AS VARCHAR)
Scott PletcherSenior DBACommented:

If the time could be more than 24 hours,  we'd need to add another calc in front to add "n day(s)" if needed.
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

ste5anSenior DeveloperCommented:

SELECT  T.create_date ,
        T.modify_date ,
        DATEDIFF(SECOND, T.create_date, T.modify_date) ,
        DATEDIFF(SECOND, T.create_date, T.modify_date) / 60 ,
        DATEDIFF(SECOND, T.create_date, T.modify_date) % 60 ,
        CAST(DATEDIFF(SECOND, T.create_date, T.modify_date) / 60 AS NVARCHAR(255)) + ':'
        + CAST(DATEDIFF(SECOND, T.create_date, T.modify_date) % 60 AS NVARCHAR(255))
FROM    sys.tables T;

Open in new window

But you better apply such a format in your frontend.
Jimbo99999Author Commented:
Thanks everyone for the responses.  I am trying them out now.
Jimbo99999Author Commented:
Thanks...it is working now!
Jim HornMicrosoft SQL Server Data DudeCommented:
Thanks for the split.  Good luck with your project.  -Jim

btw What was your eventual solution?  Reason I ask is because I have an article in progress on SQL Server date expressions, and am looking for material.
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.