Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

NULL values and RPAD/LPAD

Posted on 2000-02-22
5
Medium Priority
?
7,688 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 200 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

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

730 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