7

In T-SQL, SPACE() function is used to add spaces to a string. For e.g.

@s = 'He' + space(5) + 'llo'

Output

He     llo

So is there any function in PL/SQL that is equivalent to SPACE()?

Thank you.

3 Answers 3

11

You can use RPAD or LPAD functions

select 'He'  || rpad(' ',5,' ') || 'llo'
from dual;
/

or in PL/SQL it would be:

declare
  x varchar2(20);
begin
  x:= 'He'  || rpad(' ',5,' ') || 'llo';
end;
/
3

Jeffrey using rpad(' ',n,' ') gives n+1 spaces

select RPAD('A',3,'-')||RPAD(' ',4,' ')||RPAD('B',5,'-') from dual

Output

A--    B----

After A-- and before B, you will find 5 spaces instead of 4.

0

In Oracle, you can emulate the space() function with:

rpad(' ',n,' ')

Where n is one less than the total length of the string of spaces you want.

E.g. for a string of 40 spaces:

select rpad(' ',39,' ') as "40 spaces" from dual

dual is a dummy table you can use to test calculations and string manipulations without extracting from a real life table.

1
  • 3
    try again... select length(rpad(' ',39,' ')) from dual; returns 39, not 40. Jan 18, 2012 at 4:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.