本文介绍了如何在不使用REVERSE()函数的情况下在Oracle(11g)SQL中反转字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在不使用REVERSE函数的情况下反转字符串.我碰到了一个例子,就像这样:

I am trying to reverse a string without using REVERSE function. I came across one example which is something like:

select listagg(letter) within group(order by lvl)
from 
 (SELECT LEVEL lvl, SUBSTR ('hello', LEVEL*-1, 1) letter 
 FROM   dual 
 CONNECT BY LEVEL <= length('hello'));

除此方法外,还有其他更好的方法吗?

Apart from this approach,is there any other better approach to do this?

推荐答案

如果要避免使用未记录的reverse()函数,则可以使用 utl_raw.reverse()函数,并且也要进行适当的转换并从RAW转换:

If you're trying to avoid the undocumented reverse() function you could use the utl_raw.reverse() function instead, with appropriate conversion too and from RAW:

select utl_i18n.raw_to_char(
  utl_raw.reverse(
    utl_i18n.string_to_raw('Some string', 'AL32UTF8')), 'AL32UTF8')
from dual;

UTL_I18N.RAW_TO_CHAR(UTL_RAW.REVERSE(UTL_I18N.STRING_TO_RAW('SOMESTRING','AL32UT
--------------------------------------------------------------------------------
gnirts emoS                                                                     

这就是原始值;对此进行utl_i18n.string_to_raw();然后将其传递给utl_raw.reverse();然后将结果通过utl_i18n.raw_to_char()传回.

So that is taking an original value; doing utl_i18n.string_to_raw() on that; then passing that to utl_raw.reverse(); then passing the result of that back through utl_i18n.raw_to_char().

不确定要如何处理多字节字符,或者您想对这些字符发生什么...

Not entirely sure how that will cope with multibyte characters, or what you'd want to happen to those anyway...

或者是与@RahulTripathi链接的讨论的变体,但没有字符设置处理:

Or a variation from the discussion @RahulTripathi linked to, without the character set handling:

select utl_raw.cast_to_varchar2(utl_raw.reverse(utl_raw.cast_to_raw('Some string')))
from dual;

UTL_RAW.CAST_TO_VARCHAR2(UTL_RAW.REVERSE(UTL_RAW.CAST_TO_RAW('SOMESTRING')))   
--------------------------------------------------------------------------------
gnirts emoS                                                                     

但是该线程还指出,它仅适用于单字节字符.

But that thread also notes it only works for single-byte characters.

这篇关于如何在不使用REVERSE()函数的情况下在Oracle(11g)SQL中反转字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 15:00