There is nothing in the FME universe that has me pulling my hairs out as much over and over again as DateTime formatting and conversion.
I realize I might be be at one extreme end of the FME user spectrum in that In connect to a very small subset of data source/target types. 90% of my work is reading and writing to/from spatial and non-spatial SQL databases. Which means I don't have much use for the full range of date/time manipulation available in FME.
But that makes it even more annoying when I read datetime() from SQL and write datetime() to SQL and suddenly find myself puzzling why any formatting or transformation is necessary!? Then, there are just as many occasions when this does not happen and I immediately forget the steps I took to get things to work.
As a recent example, I'm reading a number of date fields using:
- FeatureReader, Type: Microsoft SQL Server Non-Spatial
Then I write some of this data back to SQL using:
- SQL Executor: Microsoft SQL Server Non-Spatial
Now why this wouldn't be a straight source-target read-write translation without any need to even touch the date fields, puzzles me. But I understand that FME will read datetime() and convert it to string. Which means at some point before writing back to a SQL database, I need to convert. string back to datetime.
I tried doing so using various permutations of the following inside my SQL statement in the SQL Executor:
@Value(@DateTimeParse(@Value(MYDATE),%Y-%m-%d-%H:%M:%S))
Thinking this would create something SQL Server would understand. But it only resulted in all my dates defaulting to: "1900-01-01 00:00:00.0".
What eventually did work was inserting a DateTimeConverter in front of the SQL Executor and telling it to expect dates in FME input format and to convert them to:
%Y-%m-%dT%H:%M:%S
Why is it that this works in the DateTimeConverter but not with DateTimeParse, is the latter the wrong method?
This of course, blows up for any NULL values. Telling the Converter to passthrough Null, creates more "1900-01-01 00:00:00.0" with my SQL statement. So I tried various variations of Testers or CASE statements inside the SQL. Finally, I settled for a few lines of "cleanup" SQL after the main SQL statement that just replaces all "1900-01-01" dates with NULL.
Should this have been such a meandering odyssey of trial and error? Haha. I guess what I'm getting at is that I suspect there is a better way and I want to know what that is. I love working with FME and usually find work-around for problems I run into. In fact, it' rarely the more complex workbenches that cause headaches. It's these little day-to-day head scratches that get in the way. It drives me bonkers when I don't know why FME does what it does and I have to rely on work-arounds that I'm not sure are really needed.
QUESTIONS
- How do you convert the FME datetime string correctly to something SQL Server will accept and do so inside the writer?
- What is the simplest way to get FME to leave NULL datetimes alone and just insert NULL?
- Under what circumstances (I realize this is broad), does this issue even raise its ugly head? I seem to build workbenches all the time that move dates around and never run into any of this?
Thanks for any feedback or insights or link to helpful explainers. (I'm sure I've read 9/10 FME pages on this subject and still think I'm missing something.)