Date Value calculation issues

  • I was helping one of my new employees with a script that was being written to determine date value data.  While trying to determine the number of days between the present and when an event occurred, the theory was that a simple Date 1 minus Date 2 would provide the requested answer (simple mathematical formula in).  The theory was proven incorrect as seen in the data below for anything with more than 30 days.  We are curious about two items:  (1) what is happening when we do a Date 1 minus Date 2, and (2) can we be certain that our days(date1) less days(date2) is the best course of action.

    with dates (SomeDate) as (
    SELECT CAST( '2002-09-16' AS DATE) FROM SYSIBM.SYSDUMMY1 UNION
    SELECT CAST( '2010-10-18' AS DATE) FROM SYSIBM.SYSDUMMY1 UNION
    SELECT CAST( '2010-12-30' AS DATE) FROM SYSIBM.SYSDUMMY1 UNION
    SELECT CAST( '1992-04-15' AS DATE) FROM SYSIBM.SYSDUMMY1 UNION
    SELECT CAST( '1998-02-04' AS DATE) FROM SYSIBM.SYSDUMMY1 UNION
    SELECT CAST( '1996-11-07' AS DATE) FROM SYSIBM.SYSDUMMY1 UNION
    SELECT CAST( '2001-08-31' AS DATE) FROM SYSIBM.SYSDUMMY1 UNION
    SELECT CAST( '2005-03-03' AS DATE) FROM SYSIBM.SYSDUMMY1 UNION
    SELECT CAST( '2017-04-02' AS DATE) FROM SYSIBM.SYSDUMMY1 UNION
    SELECT CAST( '2015-09-03' AS DATE) FROM SYSIBM.SYSDUMMY1)

    select
    SomeDate,
    current date - SomeDate as Value1,
    days(current date) - days(SomeDate)as DaysVal,
    SomeDate + (current date - SomeDate) days as Check1,
    SomeDate + (days(current date)- days(SomeDate)) days as Check2
    from dates

    SOMEDATEVALUE1DAYSVALCHECK1CHECK2
    4/15/1992250005913610/11/26764/20/2017
    11/7/1996200513746911/2/25454/20/2017
    2/4/1998190216701511/21/25184/20/2017
    8/31/200115072057114/28/24144/20/2017
    9/16/2002140704533012/11/23874/20/2017
    3/3/200512011744311/15/23344/20/2017
    10/18/20106060223769/19/21764/20/2017
    12/30/20106032123032/24/21764/20/2017
    9/3/2015107175951/5/20454/20/2017

    4/2/201718184/20/20174/20/2017

    Greg
    _________________________________________________________________________________________________
    The glass is at one half capacity: nothing more, nothing less.

  • Stop using the subtraction of one date from another, and compare your results with using the DATEDIFF function, which operates like this:

    DATEDIFF(day, EarlierDateFieldValue, LaterDateFieldValue)

    It might hold the key to seeing where the problem is coming from.

    Steve (aka sgmunson) 🙂 🙂 🙂
    Rent Servers for Income (picks and shovels strategy)

  • Greg Snidow - Thursday, April 20, 2017 11:22 AM

    I was helping one of my new employees with a script that was being written to determine date value data.  While trying to determine the number of days between the present and when an event occurred, the theory was that a simple Date 1 minus Date 2 would provide the requested answer (simple mathematical formula in).  The theory was proven incorrect as seen in the data below for anything with more than 30 days.  We are curious about two items:  (1) what is happening when we do a Date 1 minus Date 2, and (2) can we be certain that our days(date1) less days(date2) is the best course of action.

    with dates (SomeDate) as (
    SELECT CAST( '2002-09-16' AS DATE) FROM SYSIBM.SYSDUMMY1 UNION
    SELECT CAST( '2010-10-18' AS DATE) FROM SYSIBM.SYSDUMMY1 UNION
    SELECT CAST( '2010-12-30' AS DATE) FROM SYSIBM.SYSDUMMY1 UNION
    SELECT CAST( '1992-04-15' AS DATE) FROM SYSIBM.SYSDUMMY1 UNION
    SELECT CAST( '1998-02-04' AS DATE) FROM SYSIBM.SYSDUMMY1 UNION
    SELECT CAST( '1996-11-07' AS DATE) FROM SYSIBM.SYSDUMMY1 UNION
    SELECT CAST( '2001-08-31' AS DATE) FROM SYSIBM.SYSDUMMY1 UNION
    SELECT CAST( '2005-03-03' AS DATE) FROM SYSIBM.SYSDUMMY1 UNION
    SELECT CAST( '2017-04-02' AS DATE) FROM SYSIBM.SYSDUMMY1 UNION
    SELECT CAST( '2015-09-03' AS DATE) FROM SYSIBM.SYSDUMMY1)

    select
    SomeDate,
    current date - SomeDate as Value1,
    days(current date) - days(SomeDate)as DaysVal,
    SomeDate + (current date - SomeDate) days as Check1,
    SomeDate + (days(current date)- days(SomeDate)) days as Check2
    from dates

    SOMEDATEVALUE1DAYSVALCHECK1CHECK2
    4/15/1992250005913610/11/26764/20/2017
    11/7/1996200513746911/2/25454/20/2017
    2/4/1998190216701511/21/25184/20/2017
    8/31/200115072057114/28/24144/20/2017
    9/16/2002140704533012/11/23874/20/2017
    3/3/200512011744311/15/23344/20/2017
    10/18/20106060223769/19/21764/20/2017
    12/30/20106032123032/24/21764/20/2017
    9/3/2015107175951/5/20454/20/2017

    4/2/201718184/20/20174/20/2017

    This seems to be specific functionality for DB2. This is a MS SQL Server site, so I can't give you much hope on getting the solution here.

    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

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

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