Solved

NULL values and RPAD/LPAD

Posted on 2000-02-22
5
6,114 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

863 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now