http://www.perlmonks.org?node_id=192410

vladb has asked for the wisdom of the Perl Monks concerning the following question:

I'm presently dealing with an application where an external vendor sends me xml data that gets then parsed by my Perl script and saved in our database for future retrieval and etc. One of the xml fields is date stamp for the data.

Unfortunately, defying my pleas for consistent date formats, the vendor keeps sending me data with date stamp strings of varying 'composition'. On one occassion, I'd get '2002-08-23 11:39:24.0', or 'Fri Aug 23 12:14:04 2002', or ... This complicates my task of formating these date strings into something that Oracle would accept (when inserting data into the database via an INSERT sql statement). In my parser configuration, I set expected date format string to 'DY MON DD HH24:MI:SS YYYY'. So, my parser would affectively work only on data files that have a date stamp string of matching format. Otherwise, Oracle would complain since the TO_DATE would always fail. For example, TO_DATE('2002-08-23 11:39:24', 'DY MON DD HH24:MI:SS YYYY') is bound to fail; whereas this should work TO_DATE('2002-08-23 11:39:24', 'YYYY-MM-DD HH24:MI:SS').

So, my question is whether anyone of you knows of any Perl method or module (I couldn't find) that would allow me to build a Oracle date format string from (such as 'YYYY-MM-DD HH24:MI:SS') a date string (such as '2002-08-23 11:39:24')?

Update: I can't predict all valid date format variations that the vendor may use. That was the only reason I asked if there's any module that could do something like this, so that I won't have to ;-).

As I look at it now, I could use Class::Date to create a date object from vendor date string and then reformat it in one format that Oracle would understand. This is actually a reverse look at my initial 'question'.
use Class::Date; # . . . # make sure that whatever date format the vendor chooses to use # the one that will come out of this parser conforms to the format # specified in the configuration. my $date_obj = new Class::Date($data_xml_tag->att('date')); $vendor_data{date} = $date_obj->strftime($CONFIG::ORACLE_DATE_FORMAT +); # . . . later in the code . . . my date_f = "TO_DATE('" . $vednor_data{date} . "','" . $CONFIG:: +ORACLE_DATE_FORMAT . ")"; # I then use date_f in my insert SQL statements ...


_____________________
# Under Construction

Replies are listed 'Best First'.
Re: Computing Oracle date format from a date string
by DamnDirtyApe (Curate) on Aug 23, 2002 at 19:49 UTC

    Date::Manip can read dates in many, many formats. This may be what you're after.


    _______________
    DamnDirtyApe
    Those who know that they are profound strive for clarity. Those who
    would like to seem profound to the crowd strive for obscurity.
                --Friedrich Nietzsche
      Yep, I threw together a quick script which might give you a starting point -- it should dump the dates out in 'YYYY-MM-DD HH24:MI:SS' format.

      #!/usr/bin/perl -w use strict; $ENV{TZ} = 'EST5EDT'; use Date::Manip; my $gDate; while(<DATA>) { $gDate = ParseDate($_); print UnixDate($gDate,'%Y-%m-%d %H:%M:%S'),"\n"; } __DATA__ 2002-08-23 11:39:24.0 Fri Aug 23 12:14:04 2002

      Update: Your timezone may be different. *Smiles*

Re: Computing Oracle date format from a date string
by hmerrill (Friar) on Aug 23, 2002 at 19:30 UTC
    This is just an idea - completely untested as I don't currently have Oracle installed.
    #!/usr/bin/perl -w use strict; #my $date = "2002-08-23 11:39:24"; #my $date = "Fri Aug 23 12:14:04 2002"; my $date = "Fri Aug 2 12:14:04 2002"; my $format; if ($date =~ /\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d/) { print "yes\n"; $format = "YYYY-MM-DD HH24:MI:SS"; } elsif ($date =~ /\w\w\w \w\w\w \d{1,2} \d\d:\d\d:\d\d \d\d\d\d/) { print "2nd one\n"; $format = "DY MON DD HH24:MI:SS YYYY"; } my $sql = qq{ INSERT INTO mytable (DATE_COL) VALUES (TO_DATE('?', '?') }; $sth = $dbh->prepare($sql) || die $dbh::errstr; $sth->execute($date, $format) || die $dbh::errstr; ### blah blah ###
    Just an idea - hope it helps.
Re: Computing Oracle date format from a date string
by fglock (Vicar) on Aug 23, 2002 at 19:20 UTC

    Do you mean like this?

    $format = 'DY MON DD ... '; # default $format = 'YYYY-MM-DD ... ' if $date =~ /^\d/; # begins with number
Re: Computing Oracle date format from a date string
by BrowserUk (Patriarch) on Aug 24, 2002 at 09:42 UTC

    Its a long time since I did any SQl and it was mostly DB2 rather than Oracle, but I seem to remember a peice of SQL something like ...'somedatestring' AS DATE... which as I recall seem to be able to handle just about any date format I threw at it. Does Oracle not have such a thing?

    I also seem to remember using something like ...CAST( 'stringdate' AS DATE) DATETIME...

    Update:I have since tried to verify my (apparently failing) memory, and have been unable to do so.

    I tried to look out some of my old DB2 manuals but it seems they got 'lost' during my last house move, and I had a mouche around on the web, also without success.

    I distinctly remember being very proud of a peice of C I wrote to pre-format TIMESTAMP fields prior to passing them on to DB2 and when I showed it to a collegue, being show something that did everything my routine did and more.

    My best guess, in the absence of any supporting evidence to the contrary, is that this must have been some local innovation (I was working inside IBM at the time) - perhaps a local grown stored procedure or something.

    Sorry for my flaky memory.

    The li'l grey cells aren't what they once were--perhaps they never were to start with!¬(


    What's this about a "crooked mitre"? I'm good at woodwork!