Solved

NULL values and RPAD/LPAD

Posted on 2000-02-22
5
6,705 Views
Last Modified: 2010-05-18
Is it possible to RPAD/LPAD a null value string for spaces and if so what is the syntax?  Example:  

column NULL1      format a2
select
' '            as NULL1,

Is it possible to put rpad or lpad and the number of spaces in this statement?  The user wants to rpad a char field in a script or lpad a numeric field the formatted number of spaces.  The script is used for populating one database to another.  Thank you.
0
Comment
Question by:msuelloyd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 4

Expert Comment

by:sudhi022299
ID: 2547711
if the column values is null, then instead of using rpad/lpad you can use null value substitution fucntion (nvl)

eg:

sql> select ename,nvl(ename,' ') from emp;

if the ename column contains any null value, the nvl(ename,' ') funtion would return me with one space in it.

Regards,
Sudhi.
0
 
LVL 4

Expert Comment

by:sudhi022299
ID: 2547724
here is an example of it. I have used the length funtion so that you can know whether it has got a space in it .

sql> select empno,ename,length(ename),nvl(ename,' '),length(nvl(ename,' ')) from emp;

Output:


    EMPNO ENAME      LENGTH(ENAME) NVL(ENAME, LENGTH(NVL(ENAME,''))
--------- ---------- ------------- ---------- ---------------------
     7369 SMITH                  5 SMITH                          5
     7499 ALLEN                  5 ALLEN                          5
     7521 WARD                   4 WARD                           4
     7566 JONES                  5 JONES                          5
     7654 MARTIN                 6 MARTIN                         6
     7698 BLAKE                  5 BLAKE                          5
     7782 CLARK                  5 CLARK                          5
     7788 SCOTT                  5 SCOTT                          5
     7839 KING                   4 KING                           4
     7844 TURNER                 6 TURNER                         6
     7876 ADAMS                  5 ADAMS                          5
     7900 JAMES                  5 JAMES                          5
     7902 FORD                   4 FORD                           4
     7934 MILLER                 6 MILLER                         6
        1                                                         1

15 rows selected.
0
 
LVL 1

Expert Comment

by:vishone
ID: 2550595

 Hi msuelloyd,

 You can use SQL*Plus features itself for this

 For example I would like to print a character
 column of width 40 and number column of
 width 10 characters, I will write the format
 command like this

 SQL> column ch1 format a40 justify left
 SQL> column no1 format 9999999.99 justify right

 And then  I will execute the query
 
 SQL> SELECT 'Format Test' ch1, 1 no1 from dual;

 Use LEFT TRUNCATE instead of LEFT for JUSTIFY
 attribute in column command to avoid wrapping.

 Ofcourse, this will work for column if it having
 null value, provided atleast one row has the
 not null value for that column.

 - Vish





0
 
LVL 35

Accepted Solution

by:
Mark Geerlings earned 50 total points
ID: 2551792
Rpad and lpad will not work by themselves on null values.  You will need to use them in combination with the nvl function.  For example:

select rpad(nvl([column_name],' '),20,' ')
from [your_table];

This will first substitute a single space if the column is null, then pad it to twenty spaces.
0
 

Author Comment

by:msuelloyd
ID: 2554522
Thank you very much!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question