Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: Testing and database date functions

by hmerrill (Friar)
on Dec 09, 2004 at 18:38 UTC ( [id://413647] : note . print w/replies, xml ) Need Help??

in reply to Testing and database date functions

    But, like I said, the Perl program uses sql with current date function in it, so I can't get the program to accept the inputs for arbitrary days of week.
I don't really know what you mean by this. What database is this for? What "current date function" are you referring to? Most database have some type of "day of the week" function - you supply the date and it tells you what the day of the week is.

For example, Oracle's current date function is SYSDATE. If my perl script did something like:

my $dbh = DBI->connect(blah blah) or die "Can't connect: $DBI::errstr"; my $sth = $dbh->prepare("SELECT SYSDATE FROM DUAL") or die "Can't prepare: $DBI::errstr";
why couldn't you change the perl script to get the day of the week from Oracle - something like this:
my $sth = $dbh->prepare(qq{ SELECT TO_CHAR(SYSDATE, 'DAY') AS DAY FROM DUAL }) or die "Can't prepare: $DBI::errstr";
In Oracle sqlplus, doing "SELECT TO_CHAR(SYSDATE, 'DAY') AS DAY_OF_THE_WEEK FROM DUAL" produces this:
Isn't that what you're looking for?


Replies are listed 'Best First'.
Re^2: Testing and database date functions
by relax99 (Monk) on Dec 09, 2004 at 20:01 UTC

    No, sorry, the code is already written. I just want to test it now. What I am trying to do is to verify that the code will run the tests correctly for every day of a week. And I don't want to make any changes to production sql while doing that.

    I was referring to a "generic" current_date() database function... kind of like pseudocode... The actual function's name could be sysdate(), now(), 'current date'... most of this stuff is database-specific as you rightly noticed.

      Ok then. I don't know of an existing module that does that, so I'm guessing you'll have to roll your own. Shouldn't be that tough - you can use the database handle function get_info. Here are the get_info numbers that can be used as parameters:
      Type Name Example A Example B ---- -------------------------- ------------ ---------------- 17 SQL_DBMS_NAME 'ACCESS' 'Oracle' 18 SQL_DBMS_VER '03.50.0000' '08.01.0721 ...' 29 SQL_IDENTIFIER_QUOTE_CHAR '`' '"' 41 SQL_CATALOG_NAME_SEPARATOR '.' '@' 114 SQL_CATALOG_LOCATION 1 2
      so you could use
      my $dbms_name = $dbh->get_info(17);
      your new function could take in the $dbh handle, and call get_info(17) to get the dbms name, and once you know the name, you can construct the database specific SQL that will get you the day of the week for that database.