Monday, September 10, 2007

Yet another way to build CSV File

SQL presents an easy way to create CSV files. Use the CHAR function to convert numeric fields to alpha format. SQL puts in the necessary minus signs and decimal points. Concatenate all the fields together to get one big comma-delimited output field.

The following SQL command is an example of the technique. Use Qshell to retrieve the data and load it into a file in the Integrated File System (IFS).

db2 "SELECT char(CUSNUM)','LSTNAM','INIT','
CITY','STATE','char(baldue) from qgpl.qcustcdt"
sed -n '/,/p' >> custdata.CSV

The CSV file looks like this:
938472 ,Henning ,G K,Dallas,TX,37.00
839283 ,Jones ,B D,Clay ,NY,500.00
392859 ,Vine ,S S,Broton,VT,439.00
938485 ,Johnson ,J A,Helen ,GA,3987.50
397267 ,Tyron ,W E,Hector,NY,.00
389572 ,Stevens ,K L,Denver,CO,58.75
846283 ,Alison ,J S,Isle ,MN,10.00
475938 ,Doe ,J W,Sutter,CA,250.00
693829 ,Thomas ,A N,Casper,WY,.00
593029 ,Williams,E D,Dallas,TX,25.00
192837 ,Lee ,F L,Hector,NY,489.50
583990 ,Abraham ,M T,Isle ,MN,500.00

It isn't necessary to run SQL under Qshell, but doing so sure makes it easy to build an IFS file.

No comments: