Re: Alter Column Position

From: greg(at)turnstep(dot)com
To: pgsql-novice(at)postgresql(dot)org
Cc: Derrick(at)grifflink(dot)com
Subject: Re: Alter Column Position
Date: 2003-12-11 03:17:19
Message-ID: 00d46ff65361db998e07ffe0dc9d5d9f@biglumber.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-novice


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


[...]
> Now, I have to change the position of a column in the DB to match the csv
> parser. I have a column in position 28 that I need to move to position 7.
>
> Anyone know how to do that?

Why not preprocess the csv file before uploading it to the DB? Here's a way
to do it in quickly in Perl. 10K rows should be no problem:

$,=',';
while(<F>) {
my @foo = split(/,/ => $_);
splice(@foo,6,0,splice(@foo,27,1));
print @foo;
}

or if your csv is not so simple (e.g. contains escaped commas):

use Text::CSV_XS;
$,=',';
my $csv = Text::CSV_XS->new();
while(<F>) {
die "Cannot parse line $.: " . $csv->error_input . "\n" unless $csv->parse($_);
my @foo = $csv->fields();
print @foo[0..5,27,6..26,28(dot)(dot)(at)foo-1]; ## alternative to split()
print "\n";
}


- --
Greg Sabino Mullane greg(at)turnstep(dot)com
PGP Key: 0x14964AC8 200312102158

-----BEGIN PGP SIGNATURE-----

iD8DBQE/1+GVvJuQZxSWSsgRAp33AKCVXqKTU8qfV585DdQw+ThRWVrF7QCgkC/V
ruF2a1JeWAECM5BGSVbJcSg=
=ZY16
-----END PGP SIGNATURE-----

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Bob Badour 2003-12-11 03:35:07 Re: Making a tree with "millions and millions" of dynamic nodes
Previous Message Chris Travers 2003-12-11 03:13:49 Re: [NOVICE] PostgreSQL Training

Browse pgsql-novice by date

  From Date Subject
Next Message Manu M P 2003-12-11 03:31:31 createuser problem
Previous Message Chris Travers 2003-12-11 03:13:49 Re: [NOVICE] PostgreSQL Training