Tuesday, August 21, 2012

Removing leading and trailing spaces from Oracle SQL*Plus spooled output

Oracle's SQL*Plus application spools fields with spaces even if you try to trim the fields, because SQL*Plus is "doing you a favor" by making the fields line up. One way to work around this is to concatenate all fields into one big field so that SQL*Plus spools one big field. However, when I tried this with some very large fields, I got an error implying that I was concatenating too many characters. So how to work around this?

Here's what I did: I let SQL*Plus spool a tab delimited file with leading and trailing spaces (trailing blanks for all fields except for a field which uses STRAGG and had one leading space). Then I created an awk script to remove the leading and trailing spaces with code similar to the following: