Perl script database date convertion


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl script database date convertion
# 1  
Old 06-14-2013
Code Perl script database date convertion

Need assistance

Below script get the output correctly

I want to convert the date format .Below is the output . Any idea ?

Code:
#!/usr/bin/perl -w

use DBI;

# Get a database handle by connecting to the database


my $db = DBI->connect( "dbi:Oracle:<databasename>","servername","password" )
    || die( $DBI::errstr . "\n" );

my $sth = $db->prepare(qq(SELECT * FROM Table Tablename WHERE (CODE = ?) ORDER BY CODE));

my @data;
my $stid = $ARGV[0];
$sth->execute($stid)             # Execute the query
    or die "Couldn't execute statement: " . $sth->errstr;
while (@data = $sth->fetchrow_array()) {
    my $val1 = $data[1];
    my $val2 = $data[2];
    my $val3 = $data[3];
    my $val4 = $data[4];
    my $val5 = $data[5];

    print "$stid $val1 $val2 $val3 \n";

}

$sth->finish;

$db->disconnect;

Code:
<station> 20-JUL-08 MAX 98
<station> 18-NOV-12 MAX 73
<station> 24-MAR-12 MAX 84

I would like to get the result as
Code:
<station> 20-06-08  MAX 98
<station> 18-11-12 MAX 73
<station> 24-03-12 MAX 84

# 2  
Old 06-14-2013
I like to use the Oracle time conversion so the shell is not burdened. Does the shell have month XXX conversion? Time for strptime(). I think that makes it awk or perl time. Or brute force string translation, perhaps case in a function.

---------- Post updated at 02:12 PM ---------- Previous update was at 01:38 PM ----------

DateTime::Format::Strptime - search.cpan.org
Man Page for strptime (opensolaris Section 3c) - The UNIX and Linux Forums

Strptime() plus mktime() is the opposite of date, goes from string to zulu time.

Man Page for mktime (opensolaris Section 3) - The UNIX and Linux Forums

But the simplest is:

TO_CHAR (datetime)
This User Gave Thanks to DGPickett For This Post:
# 3  
Old 06-17-2013
Code

Solved problem Finally found a easy way in perl
Below script can be used to convert the date format

Code:
#!/usr/bin/perl -w

use warnings;
use strict;

use Date::Manip;

my $todate  = '26-jan-13';
my $newdate = UnixDate( ParseDate($todate), '%d-%m-%Y' );

print "$newdate\n";

Result:
Code:
bash-3.00$ ./test.pl
26-01-2013

# 4  
Old 06-18-2013
Yes, to go from one date format to another, someone has to parse it to a standard form like integer seconds since 1970 GMT and then reformat it in the desired form. PERL is a great place to do this, or C. GNU date -d can handle it, too. Luckily, ParseDate does not need guidance in parsing your string, like strptime() allows with a specific parsing template (opposite of 'date', same %Y %m %d family of metastrings).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help required for Oracle database shutdown script conversion from shell to perl

Please tell me how to convert below program from shell script to perl. Same commands need to use in shutdown, just need program help for startup. export ORACLE_BASE=/home/oracle1 lsnrctl start lndb1 sqlplus '/ as sysdba' startup; (2 Replies)
Discussion started by: learnbash
2 Replies

2. Shell Programming and Scripting

Perl Script date correction

Need assistance for the below script. Little details: based on the hours specified it calculates future or past date,time. This script works absolutely great . But at one point when i wante specify future date with hours at 1682 hours ...1 hours skips .Dont know why the calculation misses 1... (2 Replies)
Discussion started by: ajayram_arya
2 Replies

3. UNIX and Linux Applications

SQL database call into Multidimensional Array using Perl Script

#!/usr/local/bin/perl use DBI; use File::Copy; use Time::Local; use Data::Dumper; -Comments Describing what I'm doing-------------- -I'm pulling information from a database that has an ID and Name. They are separated by a space and I'm trying to load them into a multidimensional array so as... (3 Replies)
Discussion started by: eazyeddie22
3 Replies

4. Programming

Help with mySQL database by perl script

Hello; I was trying to set up a mysql database using following script, but never went through. The code seems fine without any syntax error as I tested it: perl -c Brapa0101-db.pl Brapa0101-db.pl syntax OKHowever, whenever I run it, an error message was tossed out: DBD::mysql::st execute... (7 Replies)
Discussion started by: yifangt
7 Replies

5. Shell Programming and Scripting

Perl script to connect to database using JDBC driver?

How to connect to SQL Server database from perl script using JDBC driver? ---------- Post updated at 05:33 PM ---------- Previous update was at 05:07 PM ---------- i have sqljdbc.jar file. by setting the class path how can i connect to the database using perl script? (2 Replies)
Discussion started by: laknar
2 Replies

6. Shell Programming and Scripting

[Perl] script -database

Welcome. I am writing a perl script. I have to design a database consisting of a single table (any subject) saved in a text file. (I make it vi command name and I am giving permission chmod u + x?) The table should contain at least four columns, including a column containing the ID (serial number )... (4 Replies)
Discussion started by: qwerty007
4 Replies

7. Shell Programming and Scripting

Increment a date variable in perl script

Hi, I have a perl script which prints epoch value of date in milliseconds as the output. My reuirement is that once the output is printed,the day variable shld increment by 1 and when i execute the script for the second time the output shld be for the new day value. My script looks as... (11 Replies)
Discussion started by: jyothi_wipro
11 Replies

8. Shell Programming and Scripting

Convertion of Date Format using SQL query in a shell script

When I write Select date_field from TableA fetch first row only I am getting the output as 09/25/2009. I want to get the output in the below format 2009-09-25 i.e., MM-DD-YYYY. Please help (7 Replies)
Discussion started by: dinesh1985
7 Replies

9. Shell Programming and Scripting

Automating A Perl Script over a database

Dear Scripting Gods I've never done shell scripting before and have only recently got to grips with Perl, so apologies for my naivity. I've written a perl program which takes in two files as arguments (these are text documents which take in the information I need) The perl program spits out a... (1 Reply)
Discussion started by: fraizerangus
1 Replies

10. Shell Programming and Scripting

How to pass a date read from database to the shell script?

Hi i have a database table which i will query in a sqlplus session and it will either come back with a date or null? Now, what i want to do is based on the date returned i will either abort or continue with the script execution. I need to know is there a way other than spooling the date... (4 Replies)
Discussion started by: vinoo128
4 Replies
Login or Register to Ask a Question