Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

NULL values and RPAD/LPAD

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
Via a live example, show how to take different types of Oracle backups using RMAN.
Suggested Courses

886 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