SELECT
  "DHINV#" AS "dhinv#",
  CASE WHEN DHIDAT IN (DATE('0001-01-01'), DATE('9999-12-31')) THEN NULL ELSE DHIDAT END AS dhidat,
  RTRIM(DHPTCP) AS dhptcp,
  RTRIM(DHCONS) AS dhcons,
  RTRIM(DHPOST) AS dhpost,
  RTRIM("DHBCS#") AS "dhbcs#",
  RTRIM("DHSCS#") AS "dhscs#",
  RTRIM(DHBNAM) AS dhbnam,
  RTRIM(DHBPOS) AS dhbpos,
  RTRIM(DHATTN) AS dhattn,
  RTRIM(DHTRCD) AS dhtrcd,
  DHRINV AS dhrinv,
  RTRIM(DHNU12) AS dhnu12,
  DHFTAM AS dhftam,
  RTRIM(DHSNAM) AS dhsnam,
  RTRIM(DHSAD1) AS dhsad1,
  RTRIM(DHSAD2) AS dhsad2,
  RTRIM(DHSAD3) AS dhsad3,
  RTRIM(DHSAD4) AS dhsad4,
  RTRIM(DHSAD5) AS dhsad5,
  RTRIM(DHSAD6) AS dhsad6,
  RTRIM(DHSAD7) AS dhsad7,
  RTRIM(DHSAD8) AS dhsad8,
  RTRIM(DHSAD9) AS dhsad9,
  RTRIM(DHSAD10) AS dhsad10,
  RTRIM(DHSPOS) AS dhspos,
  RTRIM(DHSCTY) AS dhscty,
  RTRIM(DHSPOV) AS dhspov,
  RTRIM(DHSCRY) AS dhscry,
  RTRIM(DHCPO) AS dhcpo,
  RTRIM(DHQUOT) AS dhquot,
  RTRIM(DHCURR) AS dhcurr,
  CASE WHEN DHSDAT IN (DATE('0001-01-01'), DATE('9999-12-31')) THEN NULL ELSE DHSDAT END AS dhsdat,
  RTRIM(DHSHVI) AS dhshvi,
  RTRIM(DHSLMN) AS dhslmn,
  DHCPCT AS dhcpct,
  DHCRAT AS dhcrat,
  RTRIM(DHNU5) AS dhnu5,
  RTRIM(DHFSTL) AS dhfstl,
  RTRIM(DHPSTL) AS dhpstl,
  RTRIM(DHPPCL) AS dhppcl,
  RTRIM(DHBMOD) AS dhbmod,
  RTRIM(DHPRTS) AS dhprts,
  DHBOLN AS dhboln,
  RTRIM(DHPACK) AS dhpack,
  RTRIM(DHINCR) AS dhincr,
  "DHORD#" AS "dhord#",
  DHTOTI AS dhtoti,
  DHTOTD AS dhtotd,
  DHTPST AS dhtpst,
  DHTFST AS dhtfst,
  DHTTAX AS dhttax,
  DHTCST AS dhtcst,
  RTRIM(DHTOTL) AS dhtotl,
  DHPSTR AS dhpstr,
  RTRIM(DHSTKR) AS dhstkr,
  RTRIM(DHPROV) AS dhprov,
  RTRIM(DHCTRY) AS dhctry,
  RTRIM(DHBOLP) AS dhbolp,
  RTRIM(DHTAX) AS dhtax,
  RTRIM(DHPSTC) AS dhpstc,
  RTRIM(DHTERR) AS dhterr,
  DHHBCK AS dhhbck,
  CASE WHEN DHHDAT IN (DATE('0001-01-01'), DATE('9999-12-31')) THEN NULL ELSE DHHDAT END AS dhhdat,
  DHARYR AS dharyr,
  DHARPR AS dharpr,
  DHEXRT AS dhexrt,
  RTRIM(DHEDIS) AS dhedis,
  RTRIM(DHEDIA) AS dhedia,
  CASE WHEN DHEDAT IN (DATE('0001-01-01'), DATE('9999-12-31')) THEN NULL ELSE DHEDAT END AS dhedat,
  CASE WHEN DHADAT IN (DATE('0001-01-01'), DATE('9999-12-31')) THEN NULL ELSE DHADAT END AS dhadat,
  RTRIM(DHSTOR) AS dhstor,
  RTRIM(DHFOBC) AS dhfobc,
  RTRIM(DHSLCS) AS dhslcs,
  RTRIM(DHCRTY) AS dhcrty,
  RTRIM(DHCRDT) AS dhcrdt,
  RTRIM("DHCRD#") AS "dhcrd#",
  RTRIM(DHEXPR) AS dhexpr,
  RTRIM(DHHODR) AS dhhodr,
  DHCRNM AS dhcrnm,
  RTRIM(DHFUT1) AS dhfut1,
  RTRIM(DHFUT2) AS dhfut2,
  RTRIM(DHFUT3) AS dhfut3,
  RTRIM(DHFUT4) AS dhfut4,
  RTRIM(DHFUT5) AS dhfut5,
  DHFUT6 AS dhfut6,
  RTRIM(DHFLG1) AS dhflg1,
  RTRIM(DHFLG2) AS dhflg2,
  RTRIM(DHFLG3) AS dhflg3,
  RTRIM(DHFLG4) AS dhflg4,
  RTRIM(DHFLG5) AS dhflg5,
  RTRIM(DHCLEN) AS dhclen,
  RTRIM(DHPLNT) AS dhplnt,
  DHCOMP AS dhcomp,
  RTRIM(DHSTCN) AS dhstcn,
  RTRIM(DHDLRC) AS dhdlrc,
  RTRIM(DHDDSR) AS dhddsr,
  RTRIM(DHEREF) AS dheref,
  DHYYPP AS dhyypp,
  RTRIM(DHSTTP) AS dhsttp,
  RTRIM(DHDDIR) AS dhddir,
  RTRIM(DHDEXP) AS dhdexp,
  RTRIM(DHVSXF) AS dhvsxf,
  RTRIM(DHRBIV) AS dhrbiv,
  RTRIM(DHPUSR) AS dhpusr,
  CASE WHEN DHPDAT IN (DATE('0001-01-01'), DATE('9999-12-31')) THEN NULL ELSE DHPDAT END AS dhpdat,
  DHPTIM AS dhptim,
  RTRIM(DHREF3) AS dhref3,
  DHITIM AS dhitim,
  RTRIM(DHHLDP) AS dhhldp,
  RTRIM(DHBBORD) AS dhbbord,
  RTRIM(DHPBBORD) AS dhpbbord,
  RTRIM(DHFUT7) AS dhfut7,
  RTRIM(DHFUT8) AS dhfut8,
  RTRIM(DHFUT9) AS dhfut9,
  RTRIM(DHFUT10) AS dhfut10,
  RTRIM(DHFUT11) AS dhfut11,
  RTRIM(DHFUT12) AS dhfut12,
  RTRIM(DHFUT13) AS dhfut13,
  RTRIM(DHFUT14) AS dhfut14,
  RTRIM(DHFUT15) AS dhfut15,
  RTRIM(DHFUT16) AS dhfut16,
  RTRIM(DHFUT17) AS dhfut17,
  RTRIM(DHFUT18) AS dhfut18,
  RTRIM(DHFUT19) AS dhfut19,
  DHFUT20 AS dhfut20,
  DHFUT21 AS dhfut21,
  DHFUT22 AS dhfut22,
  DHFUT23 AS dhfut23,
  DHFUT24 AS dhfut24
FROM LGDAT.OIH
WHERE
      (DHIDAT BETWEEN '{oih_wm}' AND CURRENT_DATE)     -- new invoices by invoice date
   OR (DHPDAT BETWEEN '{oih_wm}' AND CURRENT_DATE)     -- posting flip + backdated retro-billing posted recently
   OR (DHPOST <> 'Y')                                  -- always pull unposted (~23 rows)
