Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8980877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:11:43+00:00 2026-06-15T20:11:43+00:00

I have a SQL Server table that has a Time column. The table is

  • 0

I have a SQL Server table that has a “Time” column. The table is a log table the houses status messages and timestamps for each message. The log table is inserted into via a batch file. There is an ID column that groups rows together. Each time the batch file runs it initializes the ID and writes records. What I need to do is get the elapsed time from the first record in an ID set to the last record of the same ID set. I started toying with select Max(Time) – Min(Time) from logTable where id = but couldn’t figure out how to format it correctly. I need it in HH:MM:SS.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T20:11:44+00:00Added an answer on June 15, 2026 at 8:11 pm

    SQL Server doesn’t support the SQL standard interval data type. Your best bet is to calculate the difference in seconds, and use a function to format the result. The native function CONVERT() might appear to work fine as long as your interval is less than 24 hours. But CONVERT() isn’t a good solution for this.

    create table test (
      id integer not null,
      ts datetime not null
      );
    
    insert into test values (1, '2012-01-01 08:00');
    insert into test values (1, '2012-01-01 09:00');
    insert into test values (1, '2012-01-01 08:30');
    insert into test values (2, '2012-01-01 08:30');
    insert into test values (2, '2012-01-01 10:30');
    insert into test values (2, '2012-01-01 09:00');
    insert into test values (3, '2012-01-01 09:00');
    insert into test values (3, '2012-01-02 12:00');
    

    Values were chosen in such a way that for

    • id = 1, elapsed time is 1 hour
    • id = 2, elapsed time is 2 hours, and
    • id = 3, elapsed time is 3 hours.

    This SELECT statement includes one column that calculates seconds, and one that uses CONVERT() with subtraction.

    select t.id,
           min(ts) start_time,
           max(ts) end_time,
           datediff(second, min(ts),max(ts)) elapsed_sec,
           convert(varchar, max(ts) - min(ts), 108) do_not_use
    from test t
    group by t.id;
    
    ID  START_TIME                 END_TIME                   ELAPSED_SEC  DO_NOT_USE
    1   January, 01 2012 08:00:00  January, 01 2012 09:00:00  3600         01:00:00
    2   January, 01 2012 08:30:00  January, 01 2012 10:30:00  7200         02:00:00
    3   January, 01 2012 09:00:00  January, 02 2012 12:00:00  97200        03:00:00
    

    Note the misleading “03:00:00” for the 27-hour difference on id number 3.

    Function to format elapsed time in SQL Server

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an SQL Server 2008 database with a table that has a column
I'm using SQL Server 2008 and I have a table that has a 'Status'
I have simple SQL Server 2008 table that has a column ourdate of type
I have an SQL Server table that has a column called statusdate that's an
I have an SQL Server 2005 table that has a varchar(250) field which contains
I have a SQL Server table in production that has millions of rows, and
I have a SQL Server DB that has a table of products, and another
I've got a table in SQL Server 2005 that has a datetime column holding
I have an ADOQuery that inserts a record to SQL Server 2005 table that
I use SQL Server 2008 R2 and have a table that I want no

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.