Fixed Length file from a SQL script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Fixed Length file from a SQL script
# 1  
Old 07-18-2014
Fixed Length file from a SQL script

Hi,

I have a DB2 UDB 9.7 SQL script, as follows:
I need to pass the script into Unix and generate a fixed length file from this.

Can someone kindly provide a script to achieve it?

Code:
SELECT 
CAST(COALESCE(CL_ID,'000000000') AS CHAR(9)) AS CL_ID
,STATUS
,CAST(COALESCE(PLACEMENT_CATEGORY,'') AS CHAR(3)) AS PLACEMENT_CATEGORY
,CAST(COALESCE(DOB_DT,'') AS CHAR(8)) AS DOB_DT
,CAST(COALESCE(LDSS,'') AS CHAR(3)) AS LDSS
,CAST(COALESCE(FIRST_NM,'') AS CHAR(20)) AS FIRST_NM
,CAST(COALESCE(LAST_NM,'') AS CHAR(20)) AS LAST_NM
,CAST(COALESCE(MIDDLE_INITIAL,'') AS CHAR(1)) AS MIDDLE_INITIAL
,CAST(COALESCE(STREET_NUMBER,'') AS CHAR(5)) AS STREET_NUMBER
,CAST(COALESCE(Street_nm,'') AS CHAR(20)) AS STREET_NM
,CAST(COALESCE(additional_Street_,'') AS CHAR(20)) AS additional_Street_Nm
,CAST(COALESCE(CITY_NM,'') AS CHAR(15)) AS CITY_NM
,CAST(COALESCE(STATE_CD,'') AS CHAR(2)) AS STATE_CD
,CAST(COALESCE(PA_ADR_ZIP5_NO,'') AS CHAR(5)) AS PA_ADR_ZIP5_NO
,CAST(COALESCE(SSN_NO,'000000000') AS CHAR(9)) AS SSN_NO
,CAST(COALESCE(school_name,'') AS CHAR(10)) AS school_name
FROM SL_JUN2014

# 2  
Old 07-19-2014
Tried anything yourself? What failed / went wrong?

What do you mean by "fixed length file"? Even though a program could use a x-count byte raster to read & interpret data in a file, I don't think this is a usual style / file type in *nix. You mayhap mean a text file with a fixed line length? This you can achive in SQL itself using some formatting commands, I'm sure.
# 3  
Old 07-21-2014
All i tried was:
1. Create a Jun_07142014.sql file, with the following code.
Code:
EXPORT TO SL_JUN_07142014.asc of del modified by coldel| messages SL_JUN_07142014.expmsg
SELECT
CAST(COALESCE(CL_ID,'000000000') AS CHAR(9)) AS CL_ID
,STATUS
,CAST(COALESCE(PLACEMENT_CATEGORY,'') AS CHAR(3)) AS PLACEMENT_CATEGORY
,CAST(COALESCE(DOB_DT,'') AS CHAR(8)) AS DOB_DT
,CAST(COALESCE(LDSS,'') AS CHAR(3)) AS LDSS
,CAST(COALESCE(FIRST_NM,'') AS CHAR(20)) AS FIRST_NM
,CAST(COALESCE(LAST_NM,'') AS CHAR(20)) AS LAST_NM
,CAST(COALESCE(MIDDLE_INITIAL,'') AS CHAR(1)) AS MIDDLE_INITIAL
,CAST(COALESCE(STREET_NUMBER,'') AS CHAR(5)) AS STREET_NUMBER
,CAST(COALESCE(Street_nm,'') AS CHAR(20)) AS STREET_NM
,CAST(COALESCE(additional_Street_,'') AS CHAR(20)) AS additional_Street_Nm
,CAST(COALESCE(CITY_NM,'') AS CHAR(15)) AS CITY_NM
,CAST(COALESCE(STATE_CD,'') AS CHAR(2)) AS STATE_CD
,CAST(COALESCE(PA_ADR_ZIP5_NO,'') AS CHAR(5)) AS PA_ADR_ZIP5_NO
,CAST(COALESCE(SSN_NO,'000000000') AS CHAR(9)) AS SSN_NO
,CAST(COALESCE(school_name,'') AS CHAR(10)) AS school_name
FROM SL_JUN2014;

2. Then, on the command line to generate the Jun_07142014.out file.
Code:
Jun_07142014.sql > Jun_07142014.out

3. Then open the Jun_07142014.out file and remove the double quotes and pipe (|) delimiters.

I want to be able to do all that in a script so that, when i run that script, it automatically generates the output file.
# 4  
Old 07-22-2014
A few questions:-
  • Can you extract the data you need running this from a shell script?
  • How long are the lines and how long should they be?
  • Why do you get pipes and quotes in the output? Is it in the source data?
  • Does your query language support commands such these
    • SET LINESIZE 4000
    • COLUMN x FORMAT A20

I'm still a little unclear as to what output you want. RudiC did ask, but you didn't answer. Do you want a fixed line length file, fixed width columns or are you planning to FTP this to a mainframe where you can define data as fixed record?


If you could help us to understand your need, then perhaps we will be better placed to help you with a solution.

What variations have you tried so far?



Robin

Last edited by rbatte1; 07-22-2014 at 11:57 AM..
# 5  
Old 07-22-2014
Hi Robin,

Firstly, i have a SQL script.
I'm doing a "EXPORT TO SL_JUN_07142014.asc of del modified by coldel|" before the SQL script so that, the columns are separated by a "|".
Then since, i'm directing this output to a .out file, it is wrapping each field with double quotes.
The SQL script itself has the lengths needed for each field. I want to retain those lengths; that's all.

Code:
Does your query language support commands such these
SET LINESIZE
COLUMN x FORMAT A20

I'm using DB2 UDB. I'm not sure, i follow your question.

Please let me know if i'm still not making sense.
# 6  
Old 07-22-2014
These are Oracle SQL statements. I'm not sure if they follow some greater standard, but the first tries to define the output line as 4000 characters wide (probably a bit excessive) and the second is to force each column to a specified width. If you set each column to a fixed size, then the output should be fixed width fields for each record.

Sorry if I've confused things.

Can you post a sample of what you can generate already from your script and tell us what is needs to look like? Feel free to sanitise it, but make sure that the columns still match the required length. We should be able to work with that and ignore the 'how it gets extracted' bit.



Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Fixed length to delimited file conversion

Hi All, I need to convert a fixed length file to a delimited file with , (comma). But not all columns, some of the columns in the fixed files are used as fillers and I do not need that in the output file. test_fixed_len.txt I 0515 MR 394 I 0618 MR & MRS 942 I 0618 MR & MRS... (7 Replies)
Discussion started by: member2014
7 Replies

2. Shell Programming and Scripting

Replace and Increment a value in the fixed length file

Hi Everyone, I need to increment a value in the fixed length file. The file has almost a million rows. Is there any easy way to accomplish this. Ex input file ASDSD ADSD 00000 X AAASD ADSD 00000 X SDDDD ADSD 00000 X Ouput ASDSD ADSD 00001 X AAASD ADSD 00002 X SDDDD ADSD 00003 X ... (7 Replies)
Discussion started by: saratha14
7 Replies

3. UNIX for Dummies Questions & Answers

Length of a fixed width file

I have a fixed width file of length 53. when is try to get the lengh of the record of that file i get 2 different answers. awk '{print length;exit}' <File_name> The above code gives me length 50. wc -L <File_name> The above code gives me length 53. Please clarify on... (2 Replies)
Discussion started by: Amrutha24
2 Replies

4. Shell Programming and Scripting

Concatenating fixed length lines in shell script

I have a peculiar file with record format like given below. Each line is wrapped to next lines after certain number of characters. I want to concatenate all wrapped lines into 1. Input:(wrapped after 10 columns) This is li ne1 This is li ne2 and this line is too lo ng Shortline ... (8 Replies)
Discussion started by: kmanyam
8 Replies

5. Shell Programming and Scripting

Using cut command in a fixed length file

Hi, I have a file which have set of rows and has to create separate files based on the id. Eg: 001_AHaris020 001_ATony030 002_AChris090 002_ASmit060 003_AJhon001 Output: I want three files like 001_A.txt, 002_A.txt and 003_A.txt. 001_A.txt should have ... (4 Replies)
Discussion started by: techmoris
4 Replies

6. Shell Programming and Scripting

Need awk script to compare 2 fields in fixed length file.

Need a script that manipulates a fixed length file that will compare 2 fields in that file and if they are equal write that line to a new file. i.e. If fields 87-93 = fields 119-125, then write the entire line to a new file. Do this for every line in the file. After we get only the fields... (1 Reply)
Discussion started by: Muga801
1 Replies

7. UNIX for Dummies Questions & Answers

Convert a tab delimited/variable length file to fixed length file

Hi, all. I need to convert a file tab delimited/variable length file in AIX to a fixed lenght file delimited by spaces. This is the input file: 10200002<tab>US$ COM<tab>16/12/2008<tab>2,3775<tab>2,3783 19300978<tab>EURO<tab>16/12/2008<tab>3,28523<tab>3,28657 And this is the expected... (2 Replies)
Discussion started by: Everton_Silveir
2 Replies

8. UNIX for Dummies Questions & Answers

What the command to find out the record length of a fixed length file?

I want to find out the record length of a fixed length file? I forgot the command. Any body know? (9 Replies)
Discussion started by: tranq01
9 Replies

9. Shell Programming and Scripting

use SQL loader to dump a fixed length file in to DB

consider a fixed length file 12345abcd8901 12345abcd7777 12345njdu8888 12345hdku8388 i would like to dump it in to oracle DB using sql loader 12345 in to first coloumn abcd in to second coloumn 8901 in to 3rd coloumn (2 Replies)
Discussion started by: anumkoshy
2 Replies

10. Shell Programming and Scripting

creating a fixed length output from a variable length input

Is there a command that sets a variable length? I have a input of a variable length field but my output for that field needs to be set to 32 char. Is there such a command? I am on a sun box running ksh Thanks (2 Replies)
Discussion started by: r1500
2 Replies
Login or Register to Ask a Question