Reverse a string in Oracle SQL without using REVERSE() function

The query

with strings as (select 'hello' str from dual union all
                 select 'friends' str from dual union all
                 select 'this is oraclesql.in' from dual)
select str,
       replace(sys_connect_by_path(substr (str, level*-1, 1), '~|'), '~|') rev_str 
from  strings
where  connect_by_isleaf = 1
connect by prior str = str                  --added because of running against several strings at once
           and prior sys_guid() is not null --added because of running against several strings at once 
           and level <= length(str);
           

Output

Another method

select utl_i18n.raw_to_char(
  utl_raw.reverse(
    utl_i18n.string_to_raw(' nice blog '))) as reverse_string
from dual;

output

Using reverse()

select reverse ('welcome') from dual;

output

Leave a Reply