I'm relatively new to Perl (as well as programming in general). I'm writing a Perl script to automate our monthly stats. The script below will connect to the Oracle database (Express edition 11g) and get the values from a table.

There's another script that runs everyday and adds stats to this table. So, this table has data for every day. I want to get the overall data for the last month.

When I run this script I don't get any error messages. I just get the '0 results' output. However, when I run the exact same SQL statement in the database directly i get a proper output!

It seems that there's a problem in the way I'm passing the date to the SQL statement. I've tried multiple variations of this but nothing seems to work! :(

Any help on where I'm going wrong will be highly appreciated.

use strict; use warnings; use DBI; my $dbh = DBI->connect('DBI:Oracle:SID', 'username', 'password') or di +e "Couldn't connect to database: " . DBI->errstr; # # this date format is just for demo. # the actual variable will have date passed like this: # my $date = "$month/%/$year"; # where $month & $year will be calculated using DateTime # my $date = '08/%/2015'; # I've also tried other date formats like '"08/%/2015"' but nothing + seems to work! :( my $sth = $dbh->prepare('SELECT sum(v1),sum(v2),sum(v3),sum(v4),sum(v5 +) FROM some_table WHERE DATE LIKE ?'); $sth->execute($date) or die "Couldn't get values from table: " . DBI-> +errstr; my ($v1,$v2,$v3,$v4,$v5); if ($sth->rows == 0) { print "0 results\n"; exit; }else{ while (my @data = $sth_1->fetchrow_array()) { $v1 = $data[0]; $v2 = $data[1]; $v3 = $data[2]; $v4 = $data[3]; $v5 = $data[4]; } print "$v1,$v2,$v3,$v4,$v5\n"; }
--------

UPDATE

I've managed to fix this based on suggestions by runrig and SimonPratt. Thanks guys :)

Here's what I did:

1. The dates in the table are stored in Oracle's default date format. So instead of using the 'LIKE' and the % wildcard I used actual dates for searching. The new statement looks like this:

SELECT sum(EMAIL_IRT),sum(DROP_CALL_COUNT),sum(ANS_CALL_COUNT),sum(EMA +ILS_IN),sum(EMAILS_OUT) FROM CS_STATS WHERE DATE_ADDED >= TO_DATE(?,\ +'yyyy-mm-dd\') AND DATE_ADDED <= TO_DATE(?,\'yyyy-mm-dd\')

2. Set the dates between which I want to search using DateTime. The script will run 1st of every month (to get the stats for the previous month) so I'm using this piece of code to get the 1st and last dates.

my $yesterday = DateTime-> now(time_zone=>'Asia/Calcutta')->subtract( +days => 1 ); my $y_month = $yesterday->month; my $y_year = $yesterday->year; if ($y_month < 10){ $y_month = "0".$y_month; } my $finaldt1 = $y_year."-".$y_month."-01"; my $finaldt2 = $yesterday->ymd;

In reply to Problem passing date to SQL by JoeTheProgrammer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.