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

Hi!

I try to select rows from a table, where the table-column called "created_date" (TIMESTAMP-format) is between '01-JULY-02' and '01-DEC-02'. It seems like my SQL-statement just ignores my date-test-condition...

How do I do this?

Thanx...

Replies are listed 'Best First'.
Re: DBI and TIMESTAMP
by rdfield (Priest) on Feb 03, 2003 at 13:03 UTC
    Just a guess given the lack of detail, but '01-JULY-02' should probably be '01-JUL-02' - did you check any errors returned from your DBI calls? (presuming, of course, that DBI is the Database Interface that you're using)

    rdfield

Re: DBI and TIMESTAMP
by mpeppler (Vicar) on Feb 03, 2003 at 16:42 UTC
    You still haven't told us which database server you are using...

    If you are using Sybase or MS-SQL then you should be aware that TIMESTAMP has nothing to do with a time or date value, which would also cause your query to behave strangely (it's a binary counter that is guaranteed to change every time the row is updated).

    Michael

Re: DBI and TIMESTAMP
by tommyw (Hermit) on Feb 03, 2003 at 13:39 UTC

    Ignores as in "I get the same results whether I test this or not", or ignores as in "I can't get any results"

    If the former, then my best guess is that it's doing an alphabetic comparison. So 11 March 2002 does lie between 1 July 2002 and 31 Dec 2002, since alphabetic sequencing runs 1, 11, 31. Worse, July to September includes March. OTOH, this doesn't gell with the example dates you've posted.

    If you're getting no results, then my suspicion would be invalid SQL. Please post us the sample code, so we can have a look.

    --
    Tommy
    Too stupid to live.
    Too stubborn to die.

      my $statement = "SELECT id FROM comments WHERE comment_date BETWEEN '01-JUL-02' AND '01-DEC-02'";
      This sql returns every id in the table "comments", no matter what comment_date is... I
        If comment_date is indeed of type 'date' try something like:
        my $statemenet = "select id from comments where comment_date >= to_date('01-jul-2002','dd-mon-yyyy') and comment_date <= to_date('01-dec-2002',dd-mon-yyyy')";
        Running your original code and this code on an Oracle database produces the same results. For a table containing the following dates:
        CDATE --------- 01-JAN-02 01-FEB-02 01-MAR-02 01-APR-02 01-MAY-02 01-JUN-02 01-JUL-02 01-AUG-02 01-SEP-02 01-OCT-02 01-NOV-02 01-DEC-02
        the above SQL statements both produce:
        CDATE --------- 01-JUL-02 01-AUG-02 01-SEP-02 01-OCT-02 01-NOV-02 01-DEC-02
        If the comment_date is converted to varchar, the following result is produced:
        no rows selected
        So that can't be the problem. You might want to run your query through an interactive session first and see what results it produces. My thought is that you are pulling the result set from a different query...

        rdfield

        By looking at the date format and syntax, my quess is you are using Oracle. I have found problems using the Oracle default date format with comparisons. What I tend to do in DBI code is set the date format with an alter session, usually right after the connect,

        my $dbh = DBI->connect(); $dbh->do('alter session set nls_date_format = 'YYYYMMDD');

        This sets the date format for the whole session. Then you can do compares with an easier format, select id from foo where date < 20001201; ..This also makes it easy to format a data file which may be read in for updates or comparisons to a db.

        STH

Re: DBI and TIMESTAMP
by CountZero (Bishop) on Feb 03, 2003 at 16:30 UTC

    Assuming that you are using a mySQL database, have a look at the "6.2.2.2 The DATETIME, DATE, and TIMESTAMP Types"-section in the documentation. It explains all about these formats.

    Just guessing, but I think you are looking at the display format of the field, whereas you would rather test the internal value.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: DBI and TIMESTAMP
by castaway (Parson) on Feb 03, 2003 at 12:47 UTC
    How did you try to do it?
    Which Database are you using?
    I seem to recall having to surround dates with #s (#01-JULY-2002#), but that was on Access/SQL-Server. YMMV.

    C.

Re: DBI and TIMESTAMP
by Anonymous Monk on Feb 03, 2003 at 14:25 UTC
    my $statement = "SELECT id FROM comments WHERE comment_date BETWEEN '01-JUL-02' AND '01-DEC-2002";
    This sql returns all id's from table "comments", regardless of what comment_date is...
Re: DBI and TIMESTAMP
by Anonymous Monk on Feb 05, 2003 at 04:09 UTC
    Are you sure you're testing correctly for a timestamp format...it's not a date chris_piechowicz@hotmail.com