Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Convert int to time Expand / Collapse
Author
Message
Posted Friday, November 9, 2012 2:32 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, November 15, 2012 9:25 AM
Points: 3, Visits: 30
I have a call accounting database for our telephone system the duration field is int and I need to convert to time then sum. The duration would be something like 6974 and I need to display 1:56:14 in the report. Also to sum total for the extension.
Post #1383245
Posted Friday, November 9, 2012 2:45 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 6:47 AM
Points: 3,908, Visits: 8,859
I'm not sure what are you trying to do. However, a simple way to convert seconds in int to time would be like this.
SELECT CAST( DATEADD( ss, 6974, 0) AS time)

What happens when SUM is equal or greater than 86400 (seconds in one day)?

You must use the SUM on the int value as it's not supported on time or datetime.



Luis C.
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1383252
Posted Friday, November 9, 2012 3:49 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 1:41 PM
Points: 35,538, Visits: 32,127
bhelper (11/9/2012)
I have a call accounting database for our telephone system the duration field is int and I need to convert to time then sum. The duration would be something like 6974 and I need to display 1:56:14 in the report. Also to sum total for the extension.


Calculate the sum first, then convert the answer to time.

{Edit} And, yes... I see that Luis already stated that. Concurrence is always a good thing.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1383281
Posted Monday, November 12, 2012 9:50 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, November 15, 2012 9:25 AM
Points: 3, Visits: 30
This is an SQL 2000 database my query is:
SELECT EXTENSION, timestamp, CONVERT(VARCHAR,Duration / 60) + ':' + RIGHT('00' + CONVERT(VARCHAR,duration % 60),2) as DURATION
FROM CDR
WHERE (TIMESTAMP BETWEEN CONVERT(DATETIME, '2012-10-01 00:00:00', 102) AND CONVERT(DATETIME, '2012-10-31 23:59:59', 102))
The DURATION field is int datatype and the raw data is 16167. Conversion looks like 269:27 which should be 04:29:07 this is for one call.
Post #1383750
Posted Monday, November 12, 2012 10:47 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, November 15, 2012 9:25 AM
Points: 3, Visits: 30
What did I post wrong? I read your best practices Etiquette.
Post #1383780
Posted Monday, November 12, 2012 11:18 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 6:47 AM
Points: 3,908, Visits: 8,859
You only calculated minutes.
SELECT CONVERT(VARCHAR,Duration / 3600) + ':' +CONVERT(VARCHAR,(Duration %3600) / 60) + ':' + RIGHT('00' + CONVERT(VARCHAR,Duration % 60),2) as DURATION
FROM (SELECT 16167 duration)CDR

You should post in the right forum to avoid assumptions that will give you solutions that won't work for you.



Luis C.
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1383795
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse