title: T-SQL Format seconds as HH:MM:SS time
body: <p>Is there any tricky way to format seconds like hours:minutes:seconds. For example,</p>
  
  <blockquote>
    <p>3660</p>
  </blockquote>
  
  <p>seconds will be displayed as </p>
  
  <blockquote>
    <p>01h 01m 00s</p>
  </blockquote>
  
  <p>or</p>
  
  <blockquote>
    <p>01:01:00</p>
  </blockquote>
  
  <p>I am aware of the standard way of doing this:</p>
  
  <ol>
  <li>Divide all seconds on 3600 to get the hours</li>
  <li>Divide the rest seconds on 60 to get the minutes</li>
  <li>The rest are the seconds</li>
  </ol>
  
  <p>I met the following issues:</p>
  
  <ol>
  <li><p>I am not able to create separate function that do this.</p></li>
  <li><p>My code is in view using several CTEs. So, variables can be declare
  using the CTEs only.</p></li>
  <li>I am not able to use the standard solution because I will have
  results  bigger then one day - <a href="https://stackoverflow.com/questions/1262497/how-to-convert-seconds-to-hhmmss-using-t-sql">How to convert Seconds to HH:MM:SS using T-SQL</a></li>
  </ol>