2 字符函数
1. replace( 字符串1,字符串2,字符串3)
replace( char, search_string, replace_string)
功能:在“字符串1”中搜索“字符串2”,并将其替换为“字符串3”。
例如下面的命令是将所有员工名字中出现的”A”替换为”中国”。
SQL>selectreplace(ename, 'A', '中国') from scott.emp;
2. instr(C1, C2, I, J)
功能:在一个字符串中搜索指定的字符,返回发现指定的字符的位置。其中:
C1被搜索的字符串
C2希望搜索的字符串
I 搜索开始位置,默认为1
J 第J次出现,默认为1
例如下面的命令是找出”oracletraning” 第二个ra出现的位置。
SQL>Selectinstr('oracle traing' , 'ra',1,2) from dual;
3. ASCII(单个字符)
功能:返回与指定字符对应的十进制数。
SQL>Selectascii ('A') A, ascii('a') a , ascii (' ') space from dual;
说明:dual是oracle系统内部提供的一个用于临时数据计算的特殊表,它只有一列DUMMY。
4. CHR(整数)
功能:给出整数,返回对应的字符。
SQL>Selectchr(54740) zhao, chr(65) char65 from dual;
5. CONCAT(字符串1,字符串2)
功能:连接两个字符串。
Selectconcat('0532-', '96656') || '拨 0' 崂山矿泉订水 fromdual;
Selectconcat (ename, '是优秀员工') from scott.emp;
该函数和|| 的作用是一样的。
6. INITCAP(字符串)
功能:返回字符串并将字符串的第一个字母变为大写。
Selectinitcap('smith') upp from dual;
Selectinitcap(ename) ename from scott.emp;
7. LENGTH(字符串)
功能:返回字符串的长度
例如:查询雇员姓名,姓名字符长度,工资及工资数字长度。
Selectename, length(ename), sal, length(to_char(sal)) from scott.emp;
例如:请查询名字的字符长度为4的雇员
Select* from scott.emp where length(ename) =4;
Selectlength('李明') from dual; --长度为2,不区分英汉,都占1个字符
说明:
The LENGTH functionsreturn the length of char. LENGTH calculates length usingcharacters as defined by the input character set.
--返回以字符为单位的长度.
LENGTHB usesbytes instead of characters.
--返回以字节为单位的长度.
LENGTHC usesUnicode complete characters.
--返回以Unicode完全字符为单位的长度.
LENGTH2 usesUCS2 code points.
--返回以UCS2代码点为单位的长度.
LENGTH4 usesUCS4 code points.
--返回以UCS4代码点为单位的长度.
下面的例子比较了不同长度计算函数的差异:
Createtable S(a char(5), b nchar(5), c varchar(5), d nvarchar2(5));
insertinto S values('aa','aa','aa','aa');
insertinto S values('你好','你好','你好','你好');
insertinto S values('你好!','你好!','你好!','你好!');
selectlength(a), a, length(b), length(c), length(d) from s;
selectlengthb(a),a,lengthb(b),lengthb(c),lengthb(d)from s;
selectlengthc(a),a,lengthc(b),lengthc(c),lengthc(d)from s;
8. LOWER(字符串)
功能:返回字符串,并将所有的字符小写。
Selectlower('AbBbCcDd') AbBbCcDd from dual;
9. UPPER(字符串)
功能:返回字符串,并将所有的字符大写。
Selectupper('AbBbCcDd') AbBbCcDd from dual;
10. SUBSTR(string,start, count)
功能:取子字符串,从start开始,取count个。
Selectsubstr('13370840627',3,5) from dual;
例如:请把雇员名字首字母小写,其他字母大写。
Selectlower(substr(ename,1,1)) || upper(substr(ename,2,length(ename)-1)) from scott.emp;
11. RPAD和LPAD函数
功能:在列的右/左边粘贴字符
例如:显示Page1要占15个字符,不足的部分左/右边用*.占位。
Selectlpad('Page 1',15, '*.') "LPAD example " from dual;
SelectRpad('Page 1',15, '*.') "RPAD example " from dual;
12. LTRIM和RTRIM
功能:删除左边/右边出现的字符串
举例如下:
Selectltrim('Qingdao University', 'Q ') from dual;
13. SOUNDEX
功能:返回一个与给定字符串读音相同的字符串
Create table table1(xm varchar(8) );
Insert intotable1 values('weather');
Insert intotable1 values('wether');
Insert intotable1 values('goose')
Select xmfrom table1 where soundex(xm)=soundex('weather');
14. TRIM('s’from 'string')
功能:去掉指定字符串前后的某些字符。
例如:
Selecttrim(0 from 0098123400) "TRIM example " from dual;
15. To_char(datetime,string format)
功能:将日期型转换为字符串。
To_char(number,stringformat)
功能:将数值转换为字符串
例如:
Selectto_char(sysdate, 'yyyy-mm-dd hh24:mi:ss') from dual;
Selectename,to_char(sal, 'L99G999D99') from scott.emp;
说明:
9:显示数字,并忽略前面0
0:显示数字,如位数不足,则用0补齐
.:在指定位置显示小数点
,:在指定位置显示逗号
$:在数字前加美元
L:在数字前加本地货币符号
C:在数字前