Solved

NULL values and RPAD/LPAD

Posted on 2000-02-22
5
5,923 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
Comment Utility
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
Comment Utility
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
Comment Utility

 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 34

Accepted Solution

by:
Mark Geerlings earned 50 total points
Comment Utility
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
Comment Utility
Thank you very much!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Subquery in Oracle: Sub queries are one of advance queries in oracle. Types of advance queries: •      Sub Queries •      Hierarchical Queries •      Set Operators Sub queries are know as the query called from another query or another subquery. It can …
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

771 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

10 Experts available now in Live!

Get 1:1 Help Now