Programming Blog

This blog is about technical and programming questions and there solutions. I also cover programs that were asked in various interviews, it will help you to crack the coding round of various interviews

Monday 20 August 2018

Reverse a String in PL/SQL

Method 1 : By using FOR loop in PLSQL


DECLARE
   l_string1   VARCHAR2 (100) := 'deeps sti dna thgil neewteb kcuts si emit ehT';
   l_string2   VARCHAR2 (100);
BEGIN
   FOR indx IN REVERSE 1 .. LENGTH (l_string1)
   LOOP
      l_string2 := l_string2 || SUBSTR (l_string1, indx, 1);
   --dbms_output.put_line(l_string2);  
 END LOOP;

   DBMS_OUTPUT.put_line (l_string2);

END;
/



Method 2 : By using REVERSE() function available in Oracle 


declare
string1 VARCHAR2(100) :='deeps sti dna thgil neewteb kcuts si emit ehT';
begin
  SELECT REVERSE(string1) into string1 from dual;
  dbms_output.put_line(string1);
end;   

/* Since reverse function is available for SQL and is not implemented in PLSQL thats why we can't directly apply it*/

1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete