in reply to Replacing nth occurrence of string

I trust you understand the potential problem with svenXY's proposed solution: it'll screw up badly if your CSV data contains any quoted occurrences of the delimiter character (e.g. any record like  field1,"field2,quoted",field3

I don't think a regex is the right way to go here. If you cannot "ALTER" a table, why not "CREATE" a new one with the extra column wherever you want it, and then just "DROP" the old one?

Replies are listed 'Best First'.
Re^2: Replacing nth occurrence of string
by svenXY (Deacon) on Sep 23, 2005 at 15:11 UTC
    Hi graff++,

    you are completely right with your comment on my solution failing on quoted delimiters. I should have mentioned that but was tempted to provide the easy-quick-and-not-perfect-solution ;-)

    There would be ways to prevent this from happening, but as this is supposed to be a DB-like thing, your suggestion is much better (although it forces the OP to completely parse the file, recreate it and write out the data).

    Regards,
    svenXY
Re^2: Replacing nth occurrence of string
by loris (Hermit) on Sep 23, 2005 at 20:47 UTC

    Thanks for pointing out the quoted-delimiter problem. The idea to simply CREATE a new table, INSERT the old data, and then rename the new table does seem a good way to go and, as svenXY say, nicely DB-like.

    Thanks,

    loris