0

On IBM iSeries SQL, select all records with date between today and 30 days ago. Note that this date on iSeries is stored as numeric 8, not as date, timestamp, or Julian date. This is typical of many older systems.

SELECT DATFLD,
       digits(dec(year(now()-30 DAYS),4)) concat
       digits(dec(month(now()-30 DAYS),2)) concat 
       digits(dec(day(now()-30 DAYS),2)) 
FROM MyFILE
WHERE DATFLD>= 
       digits(dec(year(now()-30 DAYS),4)) concat 
       digits(dec(month(now()-30 DAYS),2)) concat
       digits(dec(day(now()-30 DAYS),2))
       and DATFLD <=
       digits(dec(year(now()),4)) concat 
       digits(dec(month(now()),2)) concat
       digits(dec(day(now()),2))

Can I get any more efficient than this?

1 Answer 1

1

Write a function (UDF) to convert between a real date and the 8 byte pseudo date. Similar to this:

CREATE FUNCTION GETMACHINEDT ( 
    IDATE DATE ) 
    RETURNS DECIMAL(8, 0)   
    LANGUAGE SQL 
    RETURN CAST ((YEAR(IDATE)) * 10000 + MONTH (IDATE) * 100 + DAY (IDATE) AS DECIMAL (8, 0)); 

Then you can write your query like so:

Select DATFLD, GETMACHINEDAT(current date - 30 days)
From MyFile
Where DATFLD Between GETMACHINEDT(current date - 30 days) and
      GETMACHINEDAT(current date) 
4
  • 2
    There's an existing library (named IDATE) of UDFs you can load here think400.dk/downloads.htm Two functions, iDate() converts from numeric to date type and ConvertToiDate() converts from date type to numeric. As John mentions, you really want to convert the (CURRENT_DATE - 30 days) to numeric with the ConvertToiDate() function as that will allow indexes to be used.
    – Charles
    Sep 11, 2015 at 15:19
  • 2
    The DIGITS() constructs in the question may be significantly faster than using the multiplication functions in the RETURN. A couple unscientific tests here showed as much as 2X improvement as the data set got larger. Also, if the function is defined DETERMINISTIC, it might be an added advantage, though consecutive calls could be hard to get with same inputs. Sep 12, 2015 at 12:04
  • Charles - thanks, not expected answer, but worth looking into. Sep 13, 2015 at 12:58
  • You may use whatever mechanism inside the function you wish, depending on performance in your environment. I'm not seeing the same latency issue as @user2338816, however I am running on an IBM i. As a matter of fact the multiplication mechanism is faster for me, though not 2-1, more like 60% faster with repeated sets of 500,000 results. I would expect string concatenation and several implied casts to be much slower than multiplication and one explicit cast
    – John Kuhns
    Sep 14, 2015 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.