I am attempting to do a select insert which takes a DECIMAL(8,0) representation of a date (DOB
), converts it into a date, and inserts it into another table.
INSERT INTO myschema.beneficiary_info
(name, address, city, state, zip, dob, spouse)
SELECT
jh.bname, jh.badd1, jh.bcity, jh.bstate, jh.bzip,
CASE
WHEN JH.BDOB8 != '0' AND JH.BDOB8 IS NOT NULL THEN
DATE(
TIMESTAMP_FORMAT(
CHAR(jh.bdob8) ,'YYYYMMDD'))
ELSE null
END,
--jh.bdob8,
jh.bspous
FROM TABLE(MYSCHEMA.REMOTE_TABLE()) JH -- function pointing to a remote database
But, about 5,000 records into it (having successfully processed dates and nulls), it fails with
[Code: -181, SQL State: 22007] [SQL0181] Value in date, time, or timestamp string not valid.
I narrowed the data down to the offending row, and found this (showing results of case statement and the actual bdob8
field:
0006 bdob8
1958-12-24 19581224
(null) 19620229
1965-02-07 19650207
The leap day appears to be causing the select ... insert
to fail.
At this point, the case statement has already successfully navigated nulls by simply passing the null to the insert. Yet for some reason, when the date/timestamp functions barf and return a null, the whole thing breaks.
Looking for ideas how to overcome the apparent inability to handle leap days...
(Using IBMi DB2 V7R3M0 L00)