Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Are you ready for SQL Server 2012 or are you still partying like it is 1999?Location of files for new database in SQL Server »
    comments

    Someone asked the following question on our forum: HHMMSS format in sysjobhistory.

    The sysjobhistory table in MSDB stores the run time of jobs in the format HHMMSS as an integer. Thus, a job that finishes in 18 minutes and 9 seconds is stored as 1809. If a job finishes in 4 seconds it's stored as 4, and another that finishes in 7 hours, 21 minutes, 33 seconds as 72133. I'm trying to get to this data and need it formated in the time data type. Thus I'm looking for 00:18:09, 00:00:04, and 07:21:33 respectively.

    This is pretty simple to do in a 2 step process

    Step 1 is to make sure that you always have 6 characters, you can do that by using the right functions and padding the total characters to 6, here is an example

    RIGHT('000000' + CONVERT(VARCHAR(6),run_duration),6 )

    Once you have that, you can use the STUFF function to inject colons in position 3 and 6, the code would look like this

    STUFF(STUFF(Value,3,0,':'),6,0,':')

    The 0 means that you don't want to replace any characters

    So to put it all together, you can use a CTE (Common Table Expression) combined with the stuff function to accomplish this job

    Here is all the code that is needed

    1. ;WITH cte AS(SELECT RIGHT('000000' + CONVERT(VARCHAR(6),run_duration),6 ) AS FormattedTime,* FROM msdb..sysjobhistory
    2. WHERE run_duration >=0)
    3.  
    4. SELECT STUFF(STUFF(FormattedTime,3,0,':'),6,0,':'),* FROM cte

    Now I don't have any jobs that run over 24 hours, I might have some of them that are 16 hours or so I am not sure if it adds to the hours or if it starts to add 2 more digits for the days

    If you have never used the STUFF function before make sure to read our wiki: Ten SQL Server Functions That You Have Ignored Until Now to learn about other seldom used functions like
    BINARY_CHECKSUM
    SIGN
    COLUMNPROPERTY
    DATALENGTH
    ASCII, CHAR,UNICODE
    NULLIF
    PARSENAME
    STUFF
    REVERSE
    GETUTCDATE

    Have fun

    About the Author

    User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
    Social SitingsTwitterFacebookLinkedInHomePageFlickrLTD RSS Feed
    416 views
    Instapaper

    No feedback yet

    Leave a comment


    Your email address will not be revealed on this site.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)