Display negative seconds in HH:MM:SS format

  • I'm calculating the difference between two text boxes which record the time in seconds. When I get negative results EG -223 seconds, how can I display this as -00:03:43?

    Cheers.

  • Didn't realize that this was for reporting services until I was posting. Maybe this T-SQL code might give you an idea.

    DECLARE @Seconds int = -223

    SELECT CASE WHEN @Seconds < 0 THEN '-' ELSE '' END +

    RIGHT( '0' + CAST( ABS( @Seconds / 3600) AS VARCHAR(2)), 2) + ':' +

    RIGHT( '0' + CAST( ABS( (@Seconds % 3600) / 60) AS VARCHAR(2)), 2) + ':' +

    RIGHT( '0' + CAST( ABS( (@Seconds % 3600) % 60) AS VARCHAR(2)), 2)

    Luis C.
    General Disclaimer:
    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?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Thanks for that. I can use that in my SQL script to get the result and display from their. Saves messing about in Reporting Services.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply