• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Guru: Date Format Confusion

    May 10, 2021 Ted Holt

    Hey, Ted:

    I use the Copy to Import File (CPYTOIMPF) command to build a CSV file to be sent to a MySQL system. My colleagues are unable to import the file because of the format of a timestamp field. CPYTOIMPF puts a hyphen between the date and time (e.g. 2021-05-10-04.36.24.849555), but the other system wants a space between them. Other than writing a custom program do you know of another way to change the format of the timestamp output?

    — Richard

    You won’t need a custom program, Richard. There’s another way to handle this requirement.

    IBM has already very thoughtfully provided us with some common date, time, and timestamp formats, but it would be impossible for IBM to foresee every format we might need. For those times, we have other tools.

    In your case, I suggest you create an SQL view that uses the TO_CHAR function to format the timestamp the way you need it.

    create view SomeView as
    ( select this, that, the_other,
             to_char(TStamp, 'yyyy-mm-dd hh.mi.ss') as TStamp
      from SomeTable);
    

    The second parameter of the TO_CHAR function lets you specify how you want the date/time value formatted. The result is a character field that will go into your stream file quite nicely.

    Now, reference the view in the CPYTOIMPF command.

    CPYTOIMPF FROMFILE(SOMEVIEW)
              TOSTMF('/somedir/somefile.csv')
              STMFCODPAG(*PCASCII)      
              RCDDLM(*CRLF)   
    

    Voilà! That’s all there is to it.

    There is a lot of confusion about dates, times, and timestamps. Another reader, whom I’ll call Bob, contacted me about the same time Richard did with a question about date formats. Bob said that his people wanted a date stored in a physical file in MM-DD-YYYY format (e.g., 05-10-2021). He was trying to find a DATFMT value to put in either the DDS of the physical file or an RPG definition (“D”) spec that would give him that format. Of course, he did not find one.

    I suspect the confusion about date/time formats is due to the fact that for many years midrange programmers had to store dates and times in numeric and alphanumeric (character) fields. (I have never worked on a system where date and time were stored together in one numeric or alpha field, but I suppose it’s been done.) In those days, we had to know how a date or time was stored in order to use it properly.

    Times have changed. Now we have the date/time data types. I pointed out to Bob that a date is a date is a date, regardless of the format specified on a DDS spec or an RPG D spec. When you define a column (field) to be of date, time, or timestamp data type, the system stores the value in an internal format, and the good news is that we don’t have to know what that internal format looks like.

    When someone says he wants a date stored in a certain format, what he likely means is that he wants to see the date in that format, and that, my friend, is more good news. If I prefer to view a certain date column in month-day-year format and you prefer to view the same date in day-month-year format, we both win!

    Bob’s problem is solved as Richard’s was.

    select to_char(SomeDate, 'mm-dd-yyyy') from SomeTable;
    

    In my opinion, literal thinking is one of the reasons many IBM i shops are stuck in the 1990s.

    RELATED STORY

    Guru: Formatting Numbers and Dates/Times/Timestamps in SQL

    Share this:

    • Share on Reddit (Opens in new window) Reddit
    • Share on Facebook (Opens in new window) Facebook
    • Share on LinkedIn (Opens in new window) LinkedIn
    • Share on X (Opens in new window) X
    • Email a link to a friend (Opens in new window) Email

    Tags: Tags: 400guru, CPYTOIMPF, CSV, FHG, Four Hundred Guru, IBM i, MySQL, SQL

    Sponsored by
    JAMS Software

    One Scheduler. IBM i, Windows, Linux, and More.

    IBM i teams trust JAMS to schedule and orchestrate jobs across every platform in their environment. Centralized visibility, cross-platform dependency management, and alerts that reach the right person before the business feels it.

    Fewer than 5% of IBM i shops run IBM i only. The rest are managing cross-platform dependencies — often without a clear picture of how they connect. JAMS draws that map, enforces those dependencies automatically, and gives your team a single place to monitor, manage, and recover when something goes wrong.

    If you are running hundreds of CL scripts and custom RPG processes, bring them as-is. JAMS runs them exactly as they do today — except now they are visible, monitored, and part of an orchestrated workflow instead of scattered across folders only one person knows about.

    No consumption-based pricing. No surprise bills when your workload spikes. You pay based on how many machines JAMS talks to — that’s it.

    Learn More → https://jamsscheduler.com/lp/ibm-i

    Share this:

    • Share on Reddit (Opens in new window) Reddit
    • Share on Facebook (Opens in new window) Facebook
    • Share on LinkedIn (Opens in new window) LinkedIn
    • Share on X (Opens in new window) X
    • Email a link to a friend (Opens in new window) Email

    Precisely To Bolster Data Governance With Infogix Buy Thoroughly Modern: IBM i Web Development Trends To Watch In the Second Half

    2 thoughts on “Guru: Date Format Confusion”

    • Ken Harding says:
      May 10, 2021 at 8:38 am

      Ted, nice work. Thanks for sharing. I always enjoy and learn something from your articles.

      Reply
    • Leslie Turner says:
      May 11, 2021 at 6:44 am

      Thanks Ted! How long have we had the TO_CHAR function?

      Reply

    Leave a ReplyCancel reply

TFH Volume: 31 Issue: 35

This Issue Sponsored By

  • UCG Technologies
  • Fresche Solutions
  • IBM
  • Computer Keyes
  • Raz-Lee Security

Table of Contents

  • Talking Shop With Power Systems Chief Stephen Leonard
  • Thoroughly Modern: IBM i Web Development Trends To Watch In the Second Half
  • Guru: Date Format Confusion
  • Precisely To Bolster Data Governance With Infogix Buy
  • IBM i PTF Guide, Volume 23, Number 19

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • More Power Systems Price Hikes, This Time They Are “Directional”
  • AI Is Not Just For Developers, It Is For Everyone At Your Company
  • Guru: Finding Data In The Forest – Exploring Three-Part Naming In SQL
  • Former IBMer’s New Book Puts The Midrange In The Spotlight
  • Have You Tried To Buy A Server Lately?
  • GenAI Is The Death Of Deterministic Project Budgeting
  • PTC Adds Support For VS Code With Implementer 12.7
  • Guru: Single Threading A Program Execution
  • As I See It: Push Back
  • IBM i PTF Guide, Volume 28, Number 21

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle