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

Hello, all--

I am writing a Perl script that will run on a Linux system to which I don't have access, reading records from a SQLserver database. I'm developing the Perl on NT and testing it against our Oracle db using DBI, trying to keep it generic so that the same code will work against both databases. I've loaded 3 records into an Oracle table to test with.

I've run into a stumbling block. One field in the SQLserver table this script will be reading from is of type DATETIME(16). I need to read in that date and compare it to dates from subsequent records, to end up with the earliest date from the table in a variable.

I don't know how to interpret/treat a DATETIME field--what format will it be in when I fetch it with code like this?:

while(my $row = $sth->fetchrow_hashref()) { ($cntct_date) = $row->{"CONTACT_DATE"};

My original approach was to initialize a variable to compare incoming dates to like this (i.e., Jan 1, 3000):

$earliest_date = "30000101";
and then compare subsequent dates to it like this using DateManip--but $earliest_date ends up with nothing in it:
$date1 = ParseDate($earliest_date); $date2 = ParseDate($cntct_date); $flag = Date_Cmp($date1,$date2); if ($flag<0) { # date1 is earlier print "$earliest_date was before $cntct_date.\n"; } elsif ($flag==0) { # the two dates are identical print "$earliest_date was THE SAME AS ($cntct_date).\n"; } else { # date2 is earlier, so capture it: print "$cntct_date was earliest--save it.\n"; $earliest_date = $cntct_date; };

Two questions:
1. Is what I'm trying to do (test a script that will run against SQLserver data in DATETIME format using an Oracle table with dates entered in VARCHAR format) even possible?
2. If so, can someone enlighten me as to how I SHOULD be doing this date comparison?

Thank you.

Replies are listed 'Best First'.
Re: Help with Dates--No Test System!
by tachyon (Chancellor) on Mar 10, 2004 at 19:40 UTC

    All databases have date manipulation functions built in. They are not entirely portable. Many people store a 'date' as unix epoch time which is a 4 byte int, easy to sort, easy to compare, easy to mamipulate. If you really want to use stringified dates and then perform essentially integer operations on them I would suggest using the RDBS to do the work for you but you will have portability issues. On SQL 7....

    You can use the DATEDIFF function. SELECT DATEDIFF(d, @HireDate, getdate()) d is a date part indicator for the day @HireDate can be an inserted parameter (like with a stored proc) or tr +aded out for a column in a regular select expression. getdate() is a function that gets the current date. You can do non platform independent SQL like: SELECT id, store_date FROM some_table WHERE store_date >= date_add(now(), interval -3 month)

    See Compare Oracle and SQL dates as Oracle is different.

    cheers

    tachyon

Re: Help with Dates--No Test System!
by bean (Monk) on Mar 10, 2004 at 19:43 UTC
    Usually this sort of thing is done in SQL:
    select min(contact_date) ...
    However, if you have your heart set on comparing the dates in Perl, you can get the dates out of the database in whatever format you like (this formatstring is for oracle and may require tweaking for sqlserver):
    select to_char( contact_date, 'YYYYMMDD' ) ...
    If you get dates in this format, you don't need to use ParseDate or Date_Cmp - earlier dates will be numerically less than later dates.
Re: Help with Dates--No Test System!
by Happy-the-monk (Canon) on Mar 10, 2004 at 19:34 UTC

    I ask three more questions:

    1. do you actually want to use the year 3000?
    2. does Oracle support dates in 3000?
    3. does Date::Manip support dates in 3000?

    I'am looking for the answer to 3...

    Sören

      I'am looking for the answer to 3...

      yes, it seems so

      Sören