How do I print a new line in PL/SQL? I'm after something similar to '\n' in the C language.
Example:
begin
dbms_output.put_line('Hi, good morning friends');
end;
I need the output is like this:
hi,
good
morning
friends
How do I print a new line in PL/SQL? I'm after something similar to '\n' in the C language.
Example:
begin
dbms_output.put_line('Hi, good morning friends');
end;
I need the output is like this:
hi,
good
morning
friends
You can concatenate the CR and LF:
chr(13)||chr(10)
(on windows)
or just:
chr(10)
(otherwise)
dbms_output.put_line('Hi,'||chr(13)||chr(10) ||'good' || chr(13)||chr(10)|| 'morning' ||chr(13)||chr(10) || 'friends');
In PL/SQL code, you can use: DBMS_OUTPUT.NEW_LINE;
Most likely you need to use this trick:
dbms_output.put_line('Hi' || chr(10) ||
'good' || chr(10) ||
'morning' || chr(10) ||
'friends' || chr(10));
dbms_output.put_line('Hi,');
dbms_output.put_line('good');
dbms_output.put_line('morning');
dbms_output.put_line('friends');
or
DBMS_OUTPUT.PUT_LINE('Hi, ' || CHR(13) || CHR(10) ||
'good' || CHR(13) || CHR(10) ||
'morning' || CHR(13) || CHR(10) ||
'friends' || CHR(13) || CHR(10) ||);
try it.
Pass the string and replace space with line break, it gives you desired result.
select replace('shailendra kumar',' ',chr(10)) from dual;
begin
dbms_output.put_line('Hi, '||CHR(10)|| 'good'||CHR(10)|| 'morning' ||CHR(10)|| 'friends');
end;
SET SERVEROUTPUT ON;
DECLARE
TYPE MESSAGE IS TABLE OF VARCHAR2 (100);
MESSAGE_ MESSAGE;
PROCEDURE write_
IS
counter INT := 0;
BEGIN
MESSAGE_ :=
MESSAGE ('Hi,',
'good',
'morning',
'friends');
FOR i IN 1 .. MESSAGE_.COUNT
LOOP
counter := counter + 1;
MESSAGE_ (counter) := MESSAGE_ (i);
DBMS_OUTPUT.PUT_LINE (MESSAGE_ (counter));
END LOOP;
END;
BEGIN
write_;
END;
If you prefer the C approach, you can use the replace function to replace c-style escape sequences in your string literal.
This should not be seen as a widely recommended solution.
declare
c_lf constant char(1) := chr(10);
begin
dbms_output.put_line(replace('hi,\ngood\nmorning\nfriends','\n',c_lf));
end;
/