日期:2014-05-17  浏览次数:20873 次

oracle pl/sql实现找到前面一个字母,怎么写呢
就是一个函数,传B,得到A
传C,得到B
传Z,得到Y

怎么实现最简单呢,我现在是一个一个判断的,很多个if语句

------解决方案--------------------
使用ascii得到字符序号
select ascii('a') value from dual
然后减一得到字符
select chr(97) value from dual

组合起来即可.
select chr(ascii('a') - 1) value from dual



------解决方案--------------------
SQL code
select chr(ascii('b') - 1) "value" from dual
/*
v
-
a

1 row selected.
*/

select chr(ascii('c') - 1) "value" from dual
/*
v
-
b

1 row selected.
*/

select chr(ascii('z') - 1) "value" from dual
/*
v
-
y

1 row selected.
*/

------解决方案--------------------
如果传为A-->前面没字母时怎么显示
------解决方案--------------------
SQL code
查看范围 

select ascii('a'), ascii('z'), ascii('A'), ascii('Z') from dual;

/**
       ASCII('A')    ASCII('Z')    ASCII('A')    ASCII('Z')
1    97    122    65    90

**/

select chr(97), chr(122), chr(65), chr(90) from dual;
/**

       CHR(97)    CHR(122)    CHR(65)    CHR(90)
1    a    z    A    Z

**/

------解决方案--------------------
探讨
如果传为A-->前面没字母时怎么显示

------解决方案--------------------
[code=SQL][/code]create or replace function f_change_alph (v_str in varchar2) return varchar2 is

v_ret varchar2(2);
begin
select translate(v_str,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','BCDEFGHIJKLMNOPQRSTUVWXYZA') into v_ret from dual;
return v_ret;
end f_change_alph;


--test
select f_change_alph('Z') res from dual;
A

------解决方案--------------------
[Quote=引用:]
[code=SQL][/code]create or replace function f_change_alph (v_str in varchar2) return varchar2 is

v_ret varchar2(2);
begin
select translate(v_str,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','BCDEFGHIJKLMNOPQRS……
[/Quote]

+1

------解决方案--------------------
SQL code

--将b当作参数传入
select decode('b','a','z',chr(ascii('b')-1)) as vaule from dual

------解决方案--------------------
可以使用TRANSLATE()函数,轻松实现。
参考代码:

SQL code

SELECT TRANSLATE(原始字符串, 'bcdefghijklmnopqrstuvwxyzaBCDEFGHIJKLMNOPQRSTUVWXYZA',
                              'abcdefghijklmnopqrstuvwxyzaABCDEFGHIJKLMNOPQRSTUVWXYZ');

------解决方案--------------------
translate函数果然牛!!!
------解决方案--------------------
用catch case吧,很好的