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. SQL Seconds to Day, Hour, Minute, Second

SQL Seconds to Day, Hour, Minute, Second

Scheduled Pinned Locked Moved Database
databasecss
5 Posts 4 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.
  • A Offline
    A Offline
    ahsansharjeel
    wrote on last edited by
    #1

    Hi Experts I have a query which needs to return the second in Day, Hour, Minute, Second format. The below code works fine when its less than a day, but does not work, when the value in second is greater than a day PRINT Convert(VarChar, DateAdd(S, 86400, 0), 108) 86400 is exactly a day and it returns 00:00:00 Can someone modify it and show me the result something like this 1:00:00:00. Thanks

    M H 2 Replies Last reply
    0
    • A ahsansharjeel

      Hi Experts I have a query which needs to return the second in Day, Hour, Minute, Second format. The below code works fine when its less than a day, but does not work, when the value in second is greater than a day PRINT Convert(VarChar, DateAdd(S, 86400, 0), 108) 86400 is exactly a day and it returns 00:00:00 Can someone modify it and show me the result something like this 1:00:00:00. Thanks

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      Take a look at MODULO from BOL! SELECT 38 / 5 AS Integer, 38 % 5 AS Remainder ; Use a case statement to detect if it is over 86399 and apply the appropriate formula. If you use it in multiple places then create your own UDF.

      Never underestimate the power of human stupidity RAH

      C 1 Reply Last reply
      0
      • M Mycroft Holmes

        Take a look at MODULO from BOL! SELECT 38 / 5 AS Integer, 38 % 5 AS Remainder ; Use a case statement to detect if it is over 86399 and apply the appropriate formula. If you use it in multiple places then create your own UDF.

        Never underestimate the power of human stupidity RAH

        C Offline
        C Offline
        cbeglobal
        wrote on last edited by
        #3

        Try the option, CONVERT(VARCHAR(10),'fieldname',112)

        M 1 Reply Last reply
        0
        • C cbeglobal

          Try the option, CONVERT(VARCHAR(10),'fieldname',112)

          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          Try replying to the op next time, he will not get notification when you reply to me!

          Never underestimate the power of human stupidity RAH

          1 Reply Last reply
          0
          • A ahsansharjeel

            Hi Experts I have a query which needs to return the second in Day, Hour, Minute, Second format. The below code works fine when its less than a day, but does not work, when the value in second is greater than a day PRINT Convert(VarChar, DateAdd(S, 86400, 0), 108) 86400 is exactly a day and it returns 00:00:00 Can someone modify it and show me the result something like this 1:00:00:00. Thanks

            H Offline
            H Offline
            Hitesh R
            wrote on last edited by
            #5

            try this :

            DECLARE @DurationSeconds INT
            DECLARE @DurationDays INT
            SET @DurationSeconds = 62110
            SET @DurationDays = @DurationSeconds /86400

            Select CASE WHEN @DurationDays > 0 THEN
            Convert(VarChar,@DurationDays)+ ':'+
            Convert(VarChar, DateAdd(S, (@DurationSeconds-(@DurationDays*86400)),0), 108)
            ELSE
            Convert(VarChar, DateAdd(S, @DurationSeconds, 0), 108)
            END

            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