oracle字符函數
1,字符串截取
select substr('abcdef',1,3) from dual
2,查找子串位置
select instr('abcfdgfdhd','fd') from dual
3,字符串連接
select 'HELLO'||'hello world' from dual;
4, 1)去掉字符串中的空格
select ltrim(' abc') s1,
rtrim('zhang ') s2,
trim(' zhang ') s3 from dual
2)去掉前導和後綴
select trim(leading 9 from 9998767999) s1,
trim(trailing 9 from 9998767999) s2,
trim(9 from 9998767999) s3 from dual;
5,返回字符串首字母的Ascii值
select ascii('a') from dual
6,返回ascii值對應的字母
select chr(97) from dual
7,計算字符串長度
select length('abcdef') from dual
8,initcap(首字母變大寫) ,lower(變小寫),upper(變大寫)
select lower('ABC') s1,
upper('def') s2,
initcap('efg') s3
from dual;
9,Replace
select replace('abc','b','xy') from dual;
10,translate
select translate('abc','b','xx') from dual; -- x是1位
11, decode[實現if ..then 邏輯] 注:第一個是表達式,最後一個是不滿足任何一個條件的值
select deptno,decode(deptno,10,'1',20,'2',30,'3','其他') from dept;
例:
select seed,account_name,decode(seed,111,1000,200,2000,0) from t_userInfo//如果seed為111,則取1000;為200,取2000;其它取0
select seed,account_name,decode(sign(seed-111),1,'big seed',-1,'little seed','equal seed') from t_userInfo//如果seed>111,則顯示大;為200,則顯示小;其它則顯示相等
12, case[實現switch ..case 邏輯]
SELECT CASE X-FIELD
WHEN X-FIELD < 40 THEN 'X-FIELD 小於 40'
WHEN X-FIELD < 50 THEN 'X-FIELD 小於 50'
WHEN X-FIELD < 60 THEN 'X-FIELD 小於 60'
ELSE 'UNBEKNOWN'
END
FROM DUAL
注:CASE語句在處理類似問題就顯得非常靈活。當只是需要匹配少量數值時,用Decode更為簡潔