Access SQL Query from Perl using times with colons

I have an sql query that I am trying to run in Perl. I have data stored every 6 hours and am trying to pull the data from a run 6 hours before the most up to date run

$sqlstatement = "Select Location FROM Forecasts WHERE RunExecuted=(SELECT max(RunExecuted) - #6:00# from Forecasts) ";

Open in new window

The #6:00# is to subtract 6 hours from the most recent executed time. I wrote this query in VBA and it works perfectly. However, in perl, the : and # causes problems because Perl is trying to read this in as a parameter. How do I get perl to ignore those symbols and run the SQL query as typed.

If i remove the - #6:00# the query works fine.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Justin MathewsCommented:
Try putting the SQL in single quotes as:

$sqlstatement = 'Select Location FROM Forecasts WHERE RunExecuted=(SELECT max(RunExecuted) - #6:00# from Forecasts) ';
kyle972Author Commented:
I tried single quotes and am still getting an error.

Anything else?
Justin MathewsCommented:
Are you using Access ODBC driver? It tried a similar query and it worked fine. Here is the sample. replace with your database path and database name:

#! c:\perl\bin\perl

use Win32::ODBC;

$DriverType = "Microsoft Access Driver (*.mdb)";
$DSN = "Win32 ODBC";
$Dir = "C:\\DB\\acc";
$DBase = "Database11.mdb";

Win32::ODBC::ConfigDSN(ODBC_ADD_DSN, $DriverType,("DSN=$DSN", "Description=Win32 ODBC Test DSN for Perl", "DBQ=$Dir\\$DBase", "DEFAULTDIR=$Dir", "UID=", "PWD=")) or die "ConfigDSN(): Could not add temporary DSN" . Win32::ODBC::Error();

$db=new Win32::ODBC($DSN) or die "couldn't ODBC $DSN because ", Win32::ODBC::Error(), "\n";

$sqlstatement = "Select Location FROM Forecasts WHERE RunExecuted=(SELECT max(RunExecuted) - #6:00# from Forecasts) ";
!$db->Sql($sqlstatement) or die "couldn't do $query because ", $db->Error(), "\n";

 my %Data = $db->DataHash();
 foreach my $key(keys(%Data)){
   print $key," -> ",$Data{$key};

Win32::ODBC::ConfigDSN(ODBC_REMOVE_DSN, $DriverType, "DSN=$DSN") or die "ConfigDSN(): Could not remove temporary DSN because ", Win32::ODBC::Error();

Open in new window

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

kyle972Author Commented:
I am using DBI.

I am now curious how to just use a colon in a normal text string that you are passing to another function.


$Var="Location : Test"


The colon will cause the function to not work properly. How would I pass the text string to the function and have it remain as one long text string.

This is the solution to my sql problem as well, but is a much simpler question.

Justin MathewsCommented:
As far as I know colon (:) does not have any special meaning in Perl and need not be escaped in a string. For example, try this and it will print the value fine:

$var="Location : Test";
test ($var);

sub test
  my $param = shift;
  print $param;

kyle972Author Commented:

   Here is an example. I am reading the contents out of a txt file and then sending an email with the contents.

If there is a : in the text file it will cause issues.

my $mailer = new Net::SMTP::TLS(  
       Hello   =>      '',  
       Port    =>      587,  
       User    =>      'xxx',  
       Password=>      'xxx';  
   $mailer->datasend("Subject: ");
   open (MYFILE, 'Export.txt'); 
while (<MYFILE>) { 
 	$string=$string."$_\n" ;


Open in new window

If you're having a problem with sending the email, then try a different mail module.  I prefer to use MIME::Lite for most of my email needs.


use strict;
use warnings;
use MIME::Lite;

open my $fh, '<', 'Export.txt' or die "failed to open 'Export.txt' $!";

$/ = undef;
my $file_contents = <$fh>;
close $fh;

MIME::Lite->send('smtp', '',
          Timeout  => 60,
          Port     => 587,
          AuthUser => 'xxx',
          AuthPass => 'xxx'

my $msg = MIME::Lite->new(
        From     => '',
        To       => '',
        Subject  => 'Your Subject',
        Data     => $file_contents,


Open in new window

Your initial question was related to your sql statement, then you switched to having an email issue.  I don't know which problem you're experiencing, but for the sql issue, have you tried using one of DBI's quote methods?
kyle972Author Commented:
I accomplished the same thing by subtracting .25. Using .25 instead of #6:00# corrected the problem.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kyle972Author Commented:
It works
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.