Click here to Skip to main content
15,885,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi guys,

i have problem in doing sum(time) in sql.

actually i getting the values, but there is some minor bug or i'm missunderstood.

i have tried this
DECLARE @tb TABLE (Opt time);
 
INSERT INTO @tb (Opt)
VALUES('23:59'), ('23:59');
 
SELECT
    Sum(DateDiff(minute, 0, Opt)) As [TotalMinutes],
    
    Convert(varchar(5), Sum(DateDiff(minute, 0, Opt)) / 60)
    + ' hours '
    + Convert(char(2), Sum(DateDiff(minute, 0, Opt)) % 60)
    + ' minutes'
    As [Description]
FROM
    @tb
;

Result:  47:58

where as i'm looking for 48:00

but if i enter '24:00' instead of '23:59', its throwing an error.

what value should i enter to get 48:00


Can any one please help me.

Thanks
Posted
Comments
Corporal Agarn 1-Dec-14 14:24pm    
By-the-way BOL has for time data type: Range 00:00:00.0000000 through 23:59:59.9999999

try these..

SQL
SELECT
    Sum(DateDiff(minute, 0, Opt)) As [TotalMinutes],

    Convert(varchar(5), Sum(DateDiff(minute, 0, Opt)+1) / 60)
    + ' hours '
    + Convert(char(2), Sum(DateDiff(minute, 0, Opt)+1) % 60)
    + ' minutes'
    As [Description]
FROM
    time_table


Updated..It's bad idea,you better see @Richard Deeming solution


SQL
SELECT
    Sum(DateDiff(minute, 0, Opt)) As [TotalMinutes],
   case when Opt='23:59' then 
    Convert(varchar(5), Sum(DateDiff(minute, 0, Opt)+1) / 60)
    + ' hours '
    + Convert(char(2), Sum(DateDiff(minute, 0, Opt)+1) % 60)
    + ' minutes'
	else
	   Convert(varchar(5), Sum(DateDiff(minute, 0, Opt)) / 60)
    + ' hours '
    + Convert(char(2), Sum(DateDiff(minute, 0, Opt)) % 60)
    + ' minutes'
	end
    As [Description]

FROM
    time_table group by Opt
 
Share this answer
 
v2
Comments
abdul subhan mohammed 1-Dec-14 13:53pm    
if i enter '12:00' then it will take '12:01'
abdul subhan mohammed 1-Dec-14 13:55pm    
throwing bad result
/\jmot 1-Dec-14 14:03pm    
yes, you are right, let me check.
abdul subhan mohammed 2-Dec-14 5:30am    
Dude,

the problem in the above is,

it is sum up only those who are 23.59 or not,
i mean separately, sum up 23.59 else non- 23.59 but not both...

for eg:
if we have, 15:00, 23:59, 23:59, then its sum only 15:00 or 48:00 but instead of 63:00
The result you're getting is correct:
  • 23 hours 59 minutes is 1439 minutes;
  • Two lots of 1439 minutes is 2878 minutes;
  • 2878 minutes is 47 hours 58 minutes;


You can't store 24:00 in a time column - the range is 00:00 to 23:59:59.9999999:

Range: 00:00:00.0000000 through 23:59:59.9999999

This makes sense, since time represents a time of day, and there isn't a 24 o'clock. :)

If you're looking to store a duration, rather than a specific time, then you'll need a different data type. You could either use an int column and store the duration in minutes, or you could store two datetime2 columns to represent the start and end times of the event, and then calculate the difference.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900