sqlperldatetimecsv

Perl, SQL query and save data to csv file with correct Datetime format


The data table has too many columns to select one by one, so I am trying to pull entire data into a file. There are a number of columns contains datetime for UTC and local time. When I used the following script, all hour information is deleted and only date is saved. How can I easily fix the code to save the entire datetime information?

In summery, all datetime data in csv file was saved as "25-FEB-15" instead of "25-FEB-15 HH:MM:SS AM -08:00"

open(OUTFILE, "> ./outputfile.csv");  

my($dbh,$sth);
$dbh = DBI->connect("xxx")

my $sqlGetEid = "

I have fixed codes according to the comments below, but the problem is not resolved yet. The DB I am using is Oracle, so MySQL based codes seems not compatible. The problem is narrow down to the datetime formats that Oracle uses, so by properly handling the format this problem can be resolved. But I am not sure which perl package I have to use to nicely handle Oracle datatime formats.

use DBI;
use DBD::Oracle qw(:ora_types);
use Compress::Zlib;
use FileHandle;
use strict;
use warnings;
use DateTime;
use Data::Dumper;
use Text::CSV_XS;
use DateTime::Format::DateParse;
use DateTime::Format::DBI;
 

close $OUTFILE;

$query->finish;
$dbh->disconnect;

Solution

  • Select each column explicitly. For the date column select a TO_DATE of the column with the format you want