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 8343911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:12:32+00:00 2026-06-09T06:12:32+00:00

I have a query select *, right( convert(varchar, cast(JOB_DONE_time-JOB_send_time as datetime), 121), 12 )

  • 0

I have a query

select
  *,
  right(
    convert(varchar, cast(JOB_DONE_time-JOB_send_time as datetime), 121),
    12
  ) [TimeTaken]
from dbo.insert_data
order by job_id desc  

and result of this query is like

JOB_ID     JOB_DONE_TIME            JOB_SEND_TIME            TimeTaken
---------  -----------------------  -----------------------  ------------
001-99900  2011-12-28 13:36:57.860  2011-12-28 13:36:57.593  00:00:00.267
001-99800  2011-12-28 13:36:57.843  2011-12-28 13:36:57.593  00:00:00.250
001-99700  2011-12-28 13:36:57.687  2011-12-28 13:36:57.547  00:00:00.140
001-99600  2011-12-28 13:36:57.593  2011-12-28 13:36:57.547  00:00:00.047
001-99500  2011-12-28 13:36:57.563  2011-12-28 13:36:57.437  00:00:00.127
001-99400  2011-12-28 13:36:57.547  2011-12-28 13:36:57.437  00:00:00.110
001-99300  2011-12-28 13:36:57.483  2011-12-28 13:36:57.377  00:00:00.107
001-99200  2011-12-28 13:36:57.437  2011-12-28 13:36:57.377  00:00:00.060
001-99100  2011-12-28 13:36:57.407  2011-12-28 13:36:57.187  00:00:00.220
001-99000  2011-12-28 13:36:57.360  2011-12-28 13:36:57.187  00:00:00.173
001-9900   2011-12-28 13:32:22.657  2011-12-28 13:32:22.500  00:00:00.127

and by this query I have got TimeTaken column. Now I want to calculate addition and avg. of column timetaken. I have tried with SUM() function but it gives me error:

Msg 8117, Level 16, State 1, Line 1 Operand data type varchar is
invalid for sum operator.

How can I calculate it???

  • 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-09T06:12:34+00:00Added an answer on June 9, 2026 at 6:12 am

    As you’ve already been told, the issue has to do with the fact that you are trying to apply AVG() or SUM() to a string column, while in fact you can only use those functions on numeric columns.

    As a very SQL Server-specific solution, you could temporarily cast the result of JOB_DONE_time - JOB_send_time to float, calculate AVG() and/or SUM() then convert the results back to datetime, then to varchar, then extract the last 12 characters, like you are doing now on the non-aggregated results of the subtraction.

    Here’s an example of what I mean:

    SELECT
      JOB_ID,
      JOB_DONE_TIME,
      JOB_SEND_TIME,
      TimeTaken      = RIGHT(CONVERT(varchar(30), CAST(    TimeTakenFP          AS datetime), 121), 12),
      AvgTimeTaken   = RIGHT(CONVERT(varchar(30), CAST(AVG(TimeTakenFP) OVER () AS datetime), 121), 12),
      TotalTimeTaken = RIGHT(CONVERT(varchar(30), CAST(SUM(TimeTakenFP) OVER () AS datetime), 121), 12)
    FROM (
      SELECT
        *,
        TimeTakenFP = CAST(JOB_DONE_TIME - JOB_SEND_TIME AS float)
      FROM insert_data
    ) s
    ;
    

    For your sample data, the above query returns the following result set:

    JOB_ID    JOB_DONE_TIME           JOB_SEND_TIME           TimeTaken    AvgTimeTaken TotalTimeTaken
    --------- ----------------------- ----------------------- ------------ ------------ --------------
    001-99900 2011-12-28 13:36:57.860 2011-12-28 13:36:57.593 00:00:00.267 00:00:00.150 00:00:01.657
    001-99800 2011-12-28 13:36:57.843 2011-12-28 13:36:57.593 00:00:00.250 00:00:00.150 00:00:01.657
    001-99700 2011-12-28 13:36:57.687 2011-12-28 13:36:57.547 00:00:00.140 00:00:00.150 00:00:01.657
    001-99600 2011-12-28 13:36:57.593 2011-12-28 13:36:57.547 00:00:00.047 00:00:00.150 00:00:01.657
    001-99500 2011-12-28 13:36:57.563 2011-12-28 13:36:57.437 00:00:00.127 00:00:00.150 00:00:01.657
    001-99400 2011-12-28 13:36:57.547 2011-12-28 13:36:57.437 00:00:00.110 00:00:00.150 00:00:01.657
    001-99300 2011-12-28 13:36:57.483 2011-12-28 13:36:57.377 00:00:00.107 00:00:00.150 00:00:01.657
    001-99200 2011-12-28 13:36:57.437 2011-12-28 13:36:57.377 00:00:00.060 00:00:00.150 00:00:01.657
    001-99100 2011-12-28 13:36:57.407 2011-12-28 13:36:57.187 00:00:00.220 00:00:00.150 00:00:01.657
    001-99000 2011-12-28 13:36:57.360 2011-12-28 13:36:57.187 00:00:00.170 00:00:00.150 00:00:01.657
    001-9900  2011-12-28 13:32:22.657 2011-12-28 13:32:22.500 00:00:00.157 00:00:00.150 00:00:01.657
    

    This query calculates the aggregated results using windowed aggregate functions, so that you could see both aggregated and non-aggregated results. I only used windowed functions because I wanted to show you how your method of displaying the time can be applied to both non-aggregated and aggregated results, so that you could see which part of the expression changes depending on what kind of the result you need to convert to the time representation. In reality you may use “normal” aggregated results (and grouping, if necessary), if that’s your requirement.

    You can play with a “live” demonstration of the query at SQL Fiddle.

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

Sidebar

Related Questions

I have the following query SELECT * FROM attend RIGHT OUTER JOIN noattend ON
In T-SQL you could have a query like: SELECT * FROM Users WHERE User_Rights
I have a query: select substr(name,7,50) as location, points,sum(if (p1=r1,10,-10))as total from dq.data group
I have this query: SELECT * From checkfinale where AssociateID = 51 AND `CompletedDate`
I have this query: select top(2) property_id_ref ,image_file ,property_name from property_master a inner join
I have query SELECT helpc.cid, COUNT(help.cid) AS children_count FROM air_help_category AS helpc LEFT JOIN
I have this query: SELECT Auctions.ID FROM Auctions INNER JOIN Products ON Auctions.ProductID =
If I have a query like: SELECT DISTINCT unnest(test) AS test FROM table ORDER
i have following query SELECT *, count(jx_commissions.commission_amount) AS summe FROM jx_members INNER JOIN jx_commissions
I have this query: SELECT p.id, r.status, r.title FROM page AS p INNER JOIN

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.