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.
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;
/
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.
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.
select length(rpad(' ',39,' ')) from dual;
returns 39, not 40.
Jan 18, 2012 at 4:59