ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Error Converting Date YYYYMMDD to MM/DD/YYYY

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Error Converting Date YYYYMMDD to MM/DD/YYYY

    We have dates stored as DECIMAL 8,0 in format YYYYMMDD. We have some "invalid" dates like 00010101 that fail converting to YYYYMMDD. So I tried to use a CASE to only convert valid dates to MM/DD/YYYY, but none of them display properly in the CASE statement (Invoice_Date2), but the same convert works fine when not in the CASE. ( Invoice_Date1​ )

    SELECT
    VARCHAR_FORMAT(DATE(TIMESTAMP_FORMAT(CHAR(CTTDTE), 'YYYYMMDD')),'MM/DD/YYYY') as Invoice_Date1,
    CASE WHEN CTTDTE < 19000101 then CTTDTE else VARCHAR_FORMAT(DATE(TIMESTAMP_FORMAT(CHAR(CTTDTE), 'YYYYMMDD')),'MM/DD/YYYY') END as Invoice_Date2,
    CTTDTE as Invoice_Date3​

    So valid dates in CTTDTE display as ++++++++++ when in the CASE statement. Thoughts ?

    Click image for larger version  Name:	image.png Views:	0 Size:	17.5 KB ID:	159470
    Last edited by MFisher; 2 weeks ago.

  • #2
    One problem is the different data types:

    CASE WHEN CTTDTE < 19000101 then CTTDTE

    ... should be ...

    CASE WHEN CTTDTE < 19000101 then char(CTTDTE)

    So that all results will be the same type (char)

    Comment


    • MFisher
      MFisher commented
      Editing a comment
      Good catch, Thanks
Working...
X