Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Converting seconds to hours and mins

Converting seconds to hours and mins

Scheduled Pinned Locked Moved Database
helpquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    si_69
    wrote on last edited by
    #1

    Hi All Perhaps someone can advise on a better solution to my problem ? I have a table full of call (telephone) records, each record has the duration of the call made the duration is in seconds i.e Call table Call id, duration, telephonenumber 1 89 0122222222222 2 135 0192929292929 3 31 0121122211111 4 5....etc etc what i need to do i go through each record and convert the duration to a time format so for call id 1, the result would be 01:29, for 2, result would be 02:15 for 3, result would be 00:31 for 4, result would be 00:05 at the moment, below is what i am doing within a cursor for each row, and although it works fine it is a really slow way of doing things, and when i have to go through a few million records you can imagine this takes quite a while Can anyone suggest a quicker alternative ? Hope that all makes sense ? Simon set @CounterVariable = 1 set @hours = 0 set @mins = 0 set @time_counter = 0 WHILE (@CounterVariable <= @dur) BEGIN --- count the hours set @CounterVariable = @CounterVariable + 1 set @time_counter = @time_counter +1 IF @time_counter = 60 BEGIN set @hours = @hours + 1 set @time_counter = 0 END END --- mins is remainder (seconds) set @mins = @time_counter IF @hours < 10 and @time_counter < 10 -- build the time string BEGIN set @dur_time = '0'+ CAST(@hours as varchar(2)) + ':' + '0' + CAST(@mins as varchar(2)) END ELSE IF @hours >= 10 and @time_counter < 10 BEGIN set @dur_time = CAST(@hours as varchar(2)) + ':' + '0' + CAST(@mins as varchar(2)) END ELSE IF @hours < 10 and @time_counter>= 10 BEGIN set @dur_time = '0'+ CAST(@hours as varchar(2)) + ':' + CAST(@mins as varchar(2)) END ELSE IF @hours >=10 and @time_counter>= 10 BEGIN set @dur_time = CAST(@hours as varchar(2)) + ':' + CAST(@mins as varchar(2)) END

    H D 2 Replies Last reply
    0
    • S si_69

      Hi All Perhaps someone can advise on a better solution to my problem ? I have a table full of call (telephone) records, each record has the duration of the call made the duration is in seconds i.e Call table Call id, duration, telephonenumber 1 89 0122222222222 2 135 0192929292929 3 31 0121122211111 4 5....etc etc what i need to do i go through each record and convert the duration to a time format so for call id 1, the result would be 01:29, for 2, result would be 02:15 for 3, result would be 00:31 for 4, result would be 00:05 at the moment, below is what i am doing within a cursor for each row, and although it works fine it is a really slow way of doing things, and when i have to go through a few million records you can imagine this takes quite a while Can anyone suggest a quicker alternative ? Hope that all makes sense ? Simon set @CounterVariable = 1 set @hours = 0 set @mins = 0 set @time_counter = 0 WHILE (@CounterVariable <= @dur) BEGIN --- count the hours set @CounterVariable = @CounterVariable + 1 set @time_counter = @time_counter +1 IF @time_counter = 60 BEGIN set @hours = @hours + 1 set @time_counter = 0 END END --- mins is remainder (seconds) set @mins = @time_counter IF @hours < 10 and @time_counter < 10 -- build the time string BEGIN set @dur_time = '0'+ CAST(@hours as varchar(2)) + ':' + '0' + CAST(@mins as varchar(2)) END ELSE IF @hours >= 10 and @time_counter < 10 BEGIN set @dur_time = CAST(@hours as varchar(2)) + ':' + '0' + CAST(@mins as varchar(2)) END ELSE IF @hours < 10 and @time_counter>= 10 BEGIN set @dur_time = '0'+ CAST(@hours as varchar(2)) + ':' + CAST(@mins as varchar(2)) END ELSE IF @hours >=10 and @time_counter>= 10 BEGIN set @dur_time = CAST(@hours as varchar(2)) + ':' + CAST(@mins as varchar(2)) END

      H Offline
      H Offline
      Hayder Marzouk
      wrote on last edited by
      #2

      Hi, Here some sql to calculate Hours, Minutes, seconds using only built in sql functions and in one sql select statement: select floor(duration / 3600) as h, floor( (duration - floor (duration /3600) )/60) as m, duration - floor(duration / 3600) * 3600 - floor( (duration - floor (duration /3600) )/60) * 60 as sec then u can concatenate all these fields HTH. Hayder marzouk

      1 Reply Last reply
      0
      • S si_69

        Hi All Perhaps someone can advise on a better solution to my problem ? I have a table full of call (telephone) records, each record has the duration of the call made the duration is in seconds i.e Call table Call id, duration, telephonenumber 1 89 0122222222222 2 135 0192929292929 3 31 0121122211111 4 5....etc etc what i need to do i go through each record and convert the duration to a time format so for call id 1, the result would be 01:29, for 2, result would be 02:15 for 3, result would be 00:31 for 4, result would be 00:05 at the moment, below is what i am doing within a cursor for each row, and although it works fine it is a really slow way of doing things, and when i have to go through a few million records you can imagine this takes quite a while Can anyone suggest a quicker alternative ? Hope that all makes sense ? Simon set @CounterVariable = 1 set @hours = 0 set @mins = 0 set @time_counter = 0 WHILE (@CounterVariable <= @dur) BEGIN --- count the hours set @CounterVariable = @CounterVariable + 1 set @time_counter = @time_counter +1 IF @time_counter = 60 BEGIN set @hours = @hours + 1 set @time_counter = 0 END END --- mins is remainder (seconds) set @mins = @time_counter IF @hours < 10 and @time_counter < 10 -- build the time string BEGIN set @dur_time = '0'+ CAST(@hours as varchar(2)) + ':' + '0' + CAST(@mins as varchar(2)) END ELSE IF @hours >= 10 and @time_counter < 10 BEGIN set @dur_time = CAST(@hours as varchar(2)) + ':' + '0' + CAST(@mins as varchar(2)) END ELSE IF @hours < 10 and @time_counter>= 10 BEGIN set @dur_time = '0'+ CAST(@hours as varchar(2)) + ':' + CAST(@mins as varchar(2)) END ELSE IF @hours >=10 and @time_counter>= 10 BEGIN set @dur_time = CAST(@hours as varchar(2)) + ':' + CAST(@mins as varchar(2)) END

        D Offline
        D Offline
        DQNOK
        wrote on last edited by
        #3

        I may be too simplistic here, but I tend to agree with Hayder marzouk (presuming I'm understand what you're trying to accomplish here). I used a slightly different formula than he did: his didn't work quite right when I tried it in Access .. probably my fault. In MSAccess, this works: CREATE PROCEDURE pcSeconds2FormattedTime( seconds INTEGER ) AS SELECT (@seconds\3600) & ':' & ((@seconds - (@seconds \3600)*3600 )\60) & ':' & @seconds - (3600*(@seconds\3600) + 60*((@seconds - (@seconds \3600)*3600 )\60)) FROM dual; (presuming you have something called "dual"). If you don't have the '\' operator (integer division), then use the FLOOR function as suggested. Also, in a real db system, use the real concatenation operator '||', not the '&' ampersand. Depending on whether the SQL compiler will do "common subexpression elimination", this may or may not run any faster than your method.

        S 1 Reply Last reply
        0
        • D DQNOK

          I may be too simplistic here, but I tend to agree with Hayder marzouk (presuming I'm understand what you're trying to accomplish here). I used a slightly different formula than he did: his didn't work quite right when I tried it in Access .. probably my fault. In MSAccess, this works: CREATE PROCEDURE pcSeconds2FormattedTime( seconds INTEGER ) AS SELECT (@seconds\3600) & ':' & ((@seconds - (@seconds \3600)*3600 )\60) & ':' & @seconds - (3600*(@seconds\3600) + 60*((@seconds - (@seconds \3600)*3600 )\60)) FROM dual; (presuming you have something called "dual"). If you don't have the '\' operator (integer division), then use the FLOOR function as suggested. Also, in a real db system, use the real concatenation operator '||', not the '&' ampersand. Depending on whether the SQL compiler will do "common subexpression elimination", this may or may not run any faster than your method.

          S Offline
          S Offline
          si_69
          wrote on last edited by
          #4

          thats great, thanks guys Simon:)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups