Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

question for SQL date with win32:odbc

by benlaw (Scribe)
on May 15, 2008 at 23:44 UTC ( [id://686824]=perlquestion: print w/replies, xml ) Need Help??

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

Hi monks,

Sorry to say , I don't whether it is sql question or perl question, anywhere I just post it..

I am using win32:odbc to run sql job, it's success when I insert record and show all record : sample output

2659 Unix iserver10 2008-05-01 03:35:00 /var 0.5 0.43 14.0 Record 2660 2660 Unix iserver10 2008-05-01 03:35:00 /zLab 1.19 0.8 33.0 Record 2661 2661 Unix iserver10 2008-05-01 03:40:00 / 0.91 0.32 65.0 Record 2662 2662 Unix iserver10 2008-05-01 03:40:00 /backupd 2.94 1.83 38.0 Record 2663 2663 Unix iserver10 2008-05-01 03:40:00 /fr_dev1 1.97 0.74 63.0 Record 2664 2664 Unix iserver10 2008-05-01 03:40:00 /fr_dev1 2.5 0.43 83.0
the record is insert by perl script and the field of "DateTime" is combined like the above "2008-05-01 03:40:00" which field is format as "yyyy-mm-dd hh:mm:ss"

I try to run the following sql but obtain error, even I can run the command within ms access sql command, I try to copy and paste to perl script and run it obtain

Error in SQL query: "select Date_time from Main where Date_Time >= Dat +eAdd("h", -2, '12/05/2008 03:35') "! Error: [-3010] [1] [0] "[Microsoft][ODBC Microsoft Access Driver] Too +few parame ters. Expected 1."

$sql = "select Date_time from Main where Date_Time >= DateAdd(\"h\", - +2, '1/05/2008 05:35') ";
The full code;
# Display a table in a database using PERL ODBC module use Win32::ODBC; $dsn = "testDB"; $table = "Main"; # ====== Check if the database can be opened correctly if (!($db = new Win32::ODBC($dsn))) { print "Error in opening DSN \"$dsn\"!\n"; print "Error: " . Win32::ODBC::Error(). "\n"; exit; } # ====== Select all fields from the given table $sql = "select Date_time from Main where Date_Time >= DateAdd(\"h\", - +2, '1/05/2008 05:35') "; # ====== Check if the SQL query is correct if ($db->Sql($sql)) { print "Error in SQL query: \"$sql\"!\n"; print "Error: " . $db->Error() . "\n"; $db->Close(); exit; } # ====== Print field data print "Content-type: text/html\n\n"; print "<html><body><pre>\n"; $count = 1; @FieldNames = $db->FieldNames(); while ($db->FetchRow()) { print "Record $count\n"; %Data = $db->DataHash(); foreach $x (@FieldNames) { #print "$x: $Data{$x}\n"; print "$Data{$x} "; } print "\n"; $count++; } $db->Close();
Is it the problem about the date format ? is there any method force the string to date format ? or I need to use other module ?

Thanks a lot

Replies are listed 'Best First'.
Re: question for SQL date with win32:odbc
by cosmicperl (Chaplain) on May 16, 2008 at 00:43 UTC
    Just a guess but shouldn't:-
    $sql = "select Date_time from Main where Date_Time >= DateAdd(\"h\", - +2, '1/05/2008 05:35') ";
    be:-
    $sql = "select Date_time from Main where Date_Time >= DateAdd(\"h\", - +2, '1/05/2008 05:35:00') ";
    or possibly:-
    $sql = "select Date_time from Main where Date_Time >= DateAdd(\"h\", - +2, '01/05/2008 05:35:00') ";
    Seems like it would explain the error

    I'd say this is more of an MSSQL question

    Lyle
      Thanks for your reply, the error still obtained.

      Really I copy & paste the sql_query to other sql software is workable rather than in my script. And really sorry that I can't clarify it's my script problem or sql problem.

      I am not familiar with MS SQL, but aren't strings supposed to be in single quotes? Check the first parameter after DateAdd(.

      $sql = "select Date_time from Main where Date_Time >= DateAdd('h', -2, + '01/05/2008 05:35:00') ";

      Just a guess. P(roba|ossi)bly wrong.

      --MidLifeXis

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://686824]
Approved by cosmicperl
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2026-02-17 05:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.