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

  • Home
  • SEARCH
  • 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 9219955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:14:09+00:00 2026-06-18T03:14:09+00:00

I am making an attendance system in which I will add monthly records of

  • 0

I am making an attendance system in which I will add monthly records of employee time in and time out. At the end of the month average time in and time out will be created.

Please tell me how to apply avg() to datatype of time(7)?

declare @tblPK table
(
    timeinat varchar(13) not null,
    timeoutat varchar(13) not null
) ;

insert into @tblPK 
select cast((onattendance) as varchar(13))ONTime, 
       cast((offattendance) as varchar(13))OFFTime 
from t_attendancedetails ;

select * from @tblPK ;

This is only giving output of all entries.

  • 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-18T03:14:10+00:00Added an answer on June 18, 2026 at 3:14 am

    Calculating average with a precision of millisecond using time(7) columns.

    select dateadd(millisecond, avg(datediff(millisecond, onattendance, offattendance)), cast('00:00' as time(7)) )
    from t_attendancedetails
    

    The query uses datediffto get the difference in milliseconds between onattendance and offattendance. Then it uses the aggregate function avg to calculate the average difference in milliseconds and finally it uses dateadd to add the average number of milliseconds to the time(7) value 00:00.

    Ref:
    DATEDIFF (Transact-SQL)
    DATEADD (Transact-SQL)
    AVG (Transact-SQL)
    CAST and CONVERT (Transact-SQL)

    Update:

    When calculating the average with a precision of time(7) you need to split the time in two parts because datediff and dateadd does not deal with bigint.

    The time difference values is converted to nanoseconds before average is calculated and the conversion back to time(7) is done in two steps, first the seconds and then the nanoseconds.

    declare @T0 time(7) = '00:00:00'
    declare @G bigint = 1000000000  
    declare @H bigint = 100         
    
    select dateadd(nanosecond, cast(right(T.A, 9) as int), dateadd(second, T.A / @G, @T0))
    from
      (
      select avg(
                  @G * datediff(second, @T0, offattendance) + @H * right(offattendance, 7) -
                  @G * datediff(second, @T0, onattendance ) + @H * right(onattendance,  7)
                )
      from t_attendancedetails
      ) as T(A)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a chart that will show attendance over a variable amount of time
Making sense out of an .MSI verbose trace. I created the .MSI using VisualStudio
Making a simple program which will generate a multiple choice form. I have an
Making a system with my group in school and got a problem figuring out
Making a simple application, so when the user logs out of Windows, it of
Making an adobe flex ui in which data that is calculated must use proprietary
Making my first steps in RIA Services (VS2010Beta2) and i encountered this problem: created
Making a form that will accept a bunch of textual data and an image
Making an Flex App. Just wondering if anyone has created something that fits automatically
making an android app to do a lottery prize system. I want to randomly

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.