• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Guru: The Deception of Fractional Labeled Durations

    August 9, 2021 Ted Holt

    Hey, Ted:

    We measure certain processes in tenths of an hour. For whatever reason, we cannot make date/time arithmetic work properly when dealing with this data. There must be something that we don’t understand — what we’re doing seems simple and straightforward. Can you help?

    — One Confused Reader

    What this reader wants to do makes perfect sense. He wants to take a value like 8:00 AM, add a fractional number of hours to it, let’s say 1.1, and come up with 9:06 AM. Let’s try an example.

    declare global temporary table StampData
     ( Stamp   timestamp );
        
    insert into session.StampData values
      ('2021-02-28-23.15.30.123456');
    
    update session.StampData as s
       set s.Stamp = s.Stamp + 1.1 hours; 
    

    Db2 executes the UPDATE without complaint, that is, there are no error messages and no warnings. What’s the new value of STAMP?

    select * from session.StampData;  
    
    Stamp
    ==========================
    2021-03-01 00:15:30.123456
    

    It’s not what we expected, is it? Why isn’t it 2021-03-01-00:21:30.123456?

    The answer is that, except for seconds, Db2 ignores the fractional portion of labeled durations. Furthermore, Db2 doesn’t bother to tell you that it has ignored those fractions.

    Let’s try something. Let’s add both whole and fractional labeled durations to a timestamp.

    select s.Stamp,
           s.Stamp + 1 year + 1 month + 1 day + 
                     1 hour + 1 minute + 1 second + 
                     1 microsecond,
           s.Stamp + 1.5 years + 1.5 months + 1.5 days + 
                     1.5 hours + 1.5 minutes + 1.5 seconds + 
                     1 microsecond
      from session.StampData as s;
    

    Here are the three values, folded one per line for ease of comparison.

    2021-02-28 23:15:30.123456	
    2022-03-30 00:16:31.123457	
    2022-03-30 00:16:31.623457
    

    Except for seconds, adding 1.5 is no different from adding 1.

    The solution is to work in smaller units of time. For fractional days, work in hours. For fractional hours, work in minutes or seconds. Adding 1.1 hours to a timestamp means multiplying 1.1 by 60 and adding 66 minutes.

    Here’s the same UPDATE.

    update session.StampData as s
       set s.Stamp = s.Stamp + 66 minutes;  
    

    or

    update session.StampData as s
       set s.Stamp = s.Stamp + 3960 seconds;  
    

    Either way, you get the desired result.

    Stamp
    ==========================
    2021-03-01 00:21:30.123456
    

    You’ll find you must do the same sort of thing when calculating durations. Use the TIMESTAMPDIFF function to find the elapsed time in a smaller unit, then divide to get the larger unit as a fraction. For example:

    declare global temporary table JobData
      ( StartTime timestamp, StopTime timestamp);
    
    insert into session.JobData values
    ('2021-08-09-08.00.00', '2021-08-09-10.18.00');
    

    How many hours did the machining operation take to run?

    select StopTime, StartTime, 
           timestampdiff (8, cast (StopTime - StartTime as char(22)))
              as Elapsed
      from JobData;
    

    The literal 8 tells TIMESTAMPDIFF to calculate elapsed time in minutes. The answer we get is 2, which is wrong.

    Instead try either of the following, both of which yield the correct answer — 2.3.

    select StopTime, StartTime, 
           (timestampdiff (4, cast (StopTime - StartTime as char(22)))
              / 60.0) as Elapsed
      from JobData;
      
    select StopTime, StartTime, 
           dec(timestampdiff (2, cast (StopTime - StartTime as char(22)))
              / 3600.0, 5,4 ) as Elapsed 
      from JobData;
    

    I don’t know why we can’t use fractions in labeled durations. Graeme Birchall, author of the Db2 SQL Cookbook, says that the reason is that fractions don’t make sense when speaking of years and months, as the number of days in a year or month varies. Maybe so, but I don’t know why that precludes the use of fractional labeled durations for days, hours, and minutes. It doesn’t preclude the use of fractional labeled durations for seconds. But no one asked my opinion.

    RELATED STORIES

    Db2 SQL Cookbook

    TIMESTAMPDIFF

    Share this:

    • Share on Reddit (Opens in new window) Reddit
    • Share on Facebook (Opens in new window) Facebook
    • Share on LinkedIn (Opens in new window) LinkedIn
    • Share on X (Opens in new window) X
    • Email a link to a friend (Opens in new window) Email

    Tags: Tags: 400guru, DB2, FHG, Four Hundred Guru, IBM i

    Sponsored by
    ARCAD Software

    [IBM i Auditing Tool] Discover the Health of Your IBM i Applications in Minutes with ARCAD iCheck-up

    How well do you really know the health of your IBM i applications? Hidden complexity, unused code, and synchronization issues can make maintenance more challenging and slow down modernization efforts.

    What if you could identify these issues in just a few clicks, without installing anything on your IBM i system?

    Meet ARCAD iCheck-up, an auditing tool designed to give IBM i teams a clear, actionable overview of their applications in minutes.

    With iCheck-up, you can:

    • Assess code complexity: Identify complex source files and prioritize refactoring efforts.
    • Detect dead code: Uncover unused code that may be cluttering your applications.
    • Spot synchronization issues: Identify inconsistencies between source files and compiled objects.
    • Track code changes: Explore modification history and better understand your application’s evolution.
    • Generate a shareable report: Get a comprehensive, easy-to-read HTML report with key metrics and insights.

    Fast, simple, and secure. iCheck-up runs from Windows, requires no installation on your IBM i system, and leaves no files or code behind. Your generated report contains metrics only, keeping your source code confidential.

    Whether you’re maintaining existing applications, planning modernization, or looking to improve code quality, iCheck-up helps you make more informed decisions.

    Ready to gain deeper insights into your IBM i applications? Discover ARCAD iCheck-up and take the first step toward better code quality today!

    Share this:

    • Share on Reddit (Opens in new window) Reddit
    • Share on Facebook (Opens in new window) Facebook
    • Share on LinkedIn (Opens in new window) LinkedIn
    • Share on X (Opens in new window) X
    • Email a link to a friend (Opens in new window) Email

    Security Alert: The Anti-Alfred E. Newman Effect Thoroughly Modern: Making Quick Wins Part Of Your Modernization Strategy

    One thought on “Guru: The Deception of Fractional Labeled Durations”

    • Ted Holt says:
      August 10, 2021 at 11:12 am

      I should have mentioned that the numeric literals in the last code figure are real numbers, not integers, so that the system will use floating point division instead of integer division. Integer division will not give the correct results.

      Reply

    Leave a ReplyCancel reply

TFH Volume: 31 Issue: 51

This Issue Sponsored By

  • IBM
  • Fresche Solutions
  • Raz-Lee Security
  • Computer Keyes
  • Eradani

Table of Contents

  • Balancing Supply And Demand For Impending Big Power10 Iron
  • Thoroughly Modern: Making Quick Wins Part Of Your Modernization Strategy
  • Guru: The Deception of Fractional Labeled Durations
  • Security Alert: The Anti-Alfred E. Newman Effect
  • Sundry IBM Announcements Of Relevance To Power Shops

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • Bob Code Assistant Gets Better Understanding Of IBM i Applications
  • IBM i In The Cloud And The Case For Reversing Replication
  • Guru: From Hiking Boots To Long-Term Memory
  • Inside IBM’s New PowerHA Cloud Offering, Full System Manager
  • IBM i PTF Guide, Volume 28, Number 39
  • Power Systems Turns In Another Great Quarter
  • Big Blue Is Right On Schedule With Fall 2026 IBM i Tech Refreshes
  • Guru: Environment Variables – Global Variables Wearing Hiking Boots
  • A New IBM i Entity Takes Shape: Power By Fortra
  • IBM i PTF Guide, Volume 28, Number 38

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle