When I query a data source directly on an SQL server, there is date and time stamp column returning in a format shown below.
2011-05-26 16:19:20
2013-01-10 15:20:35
2008-01-10 14:51:01
2013-01-10 17:40:31
2008-03-26 18:23:20
2010-03-04 16:18:14
However, when I query that same data source in a perl script and write the results to a .txt file, the date format writes as shown below.
Jun 14 2011 10:49AM
Jun 17 2010 04:23PM
Aug 13 2010 08:28AM
Jun 18 2007 08:41AM
Jul 26 2012 07:18PM
Jun 11 2010 11:37AM
Aug 20 2010 09:28AM
SQL Query:
SELECT TOP 100 PERCENT UserNM AS [User ID], MAX(EventDT) AS [Last Login Date]
FROM dbo.WFEventLog
GROUP BY UserNM
ORDER BY UserNM
Script Section for Writing to File:
open(FILE, ">cleanup.txt") || die "$!\n";
while( my $hash_ref = $sth->fetchrow_hashref ){
my $userid = "$hash_ref->{'UserID'}";
my $logindt = "$hash_ref->{'Last Login Date'}";
# dump all registered users into .csv file
print FILE "$userid \t $logindt\n";
}
close(FILE);
What I am looking for is the exact format from the SQL results to be written to the file. For some reason there is a reformat happening before that is happening that I do not want.
I’m assuming you want them in identical format. I’d see first if the database will format it for you, but seeing as I don’t know what database you’re dealing with, perl has several modules that are good for this purpose. The granddaddy of them all is Date::Manip. To format your dates in the format as output by the database, you would: