1

I want to create a fixed length flat file (separated by ','), but when a field has a null value, the record moves. Please see illustration below (incorrect Jenny and Roland records):

Source Table:

Name    Color  Balance Zip Code
------- ------ ------- --------
Melissa Orange $200.00 40240
Karl    Blue   $150.00 40884
Jenny   -null- -null-  45667
Roland  -null- $110.00 53366
Vincent Green  $285.00 45677

Output I want to get:

Correct_Ouput
----------------------------
Melissa,Orange,$200.00,40240
Karl   ,Blue  ,$150.00,40884
Jenny  ,      ,       ,45667
Roland ,      ,$110.00,53366
Vincent,Green ,$285.00,45677

Output I want to get:

Wrong_Output
----------------------------
Melissa,Orange,$200.00,40240
Karl   ,Blue  ,$150.00,40884
Jenny  ,,,45667
Roland ,,$110.00,53366
Vincent,Green ,$285.00,45677

I tried searching but I get the null to empty string result.

Please help. Thank you.

2 Answers 2

2

Use the COALESCE(column,' ') function.

For some databases, you can use IFNULL or NVL. See this web page for more details.

Sign up to request clarification or add additional context in comments.

Comments

0

You could use NVL to replace the NULL value with required number of spaces to retain the format.

For example,

SQL> SELECT 'Karl' NAME,
  2    NVL('Blue', '     ') color,
  3    NVL('$150.00','         ') balance,
  4    40884 zip_code
  5  FROM dual
  6  UNION ALL
  7  SELECT 'Jenny' name,
  8    NVL(NULL, '     ') color,
  9    NVL(NULL,'         ') balance,
 10    45667 zip_code
 11  FROM dual
 12  /

NAME  COLOR BALANCE     ZIP_CODE
----- ----- --------- ----------
Karl  Blue  $150.00        40884
Jenny                      45667

SQL>

You could also use DECODE.

For example,

DECODE(column, NULL, ' ')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.