2016-01-07
目录
一、字符函数
二、数值函数
三、转换函数
四、正则表达式函数
五、聚合函数
六、分组语句
一、字符函数
1.ASCII(x)和CHR(x)
--获得字符x的ASCII码 select ascii('a'),ascii('A'),ascii('z'),ascii(0),ascii(9) from dual; --获得ASCII码为x的字符 select chr(97),chr(65),chr(122),chr(48),chr(57) from dual;
2.CONCAT(x,y)
--将y添加到x之后 --concat()和||操作符功能相同,||可以连接多个字符串,concat()只能连接2个字符串 select concat(first_name,last_name) from customers;
3.INITCAP(x)
--将x中每个单词的首字母转换成大写 select product_id,initcap(description) from products where product_id < 4;
4.INSTR(x,find_string[,start][,occurrence])
--在x中查找find_string,返回find_string所在位置 --start开始查找的位置 --occurrence第几次出现的位置 select name,instr(name,'Science') from products where product_id = 1; select name,instr(name,'e',1,2) from products where product_id = 1; select name,instr(name,'e',-1,3) from products where product_id = 1; select customer_id,dob,instr(dob,'JAN') from customers where customer_id = 1;
5.LENGTH(x)
--获得x中字符的个数 select name,length(name) from products; select price,length(price) from products where product_id < 3;
6.LOWER(x),UPPER(x)
--LOWER(x)将x中的字母转换成小写 --UPPER(x)将x中的字母转换成大写 select upper(first_name),lower(last_name) from customers;
7.LPAD(x,width[,pad_string]),RPAD(x,width[,pad_string])
--LPAD在x的左边补齐空格,使x的总长度达到width个字符 --RPAD在x的右边补齐空格,使x的总长度达到width个字符 select rpad(name,30,'.'),lpad(price,8,'*+') from products where product_id < 4;
8.LTRIM(x[,trim_string]),RTRIM(x[,trim_string]),TRIM(trim_char FROM x)
--LTRIM从x的左边截去字符 --RTRIM从x的右边截去字符 --TRIM从x的左、右各截去字符 select ltrim(' Hello Kathy Lindsey! '), rtrim(' Hi Doreeen Oakley!abcabc', 'abc'), trim('0' from '000Hey Steve Button!00000') from dual;
9.NVL(x,value)
--x为空,返回value,否则返回x select customer_id,nvl(phone,'Unknown Phone Number') from customers;
10.NVL2(x,value1,value2)
--x非空,返回value1;否则返回value2 select customer_id,nvl2(phone,'Known','Unknown') from customers;
11.REPLACE(x,search_string,replace_string)
--在x中查找search_string,并将其替换为replace_string select replace(name,'Science','PHYSICS') from products where product_id = 1;
12.SOUNDEX(x)
--获得包含x发音的字符串,用于对英文拼写不同发音相似的单词进行比较 select last_name from customers where soundex(last_name) = soundex('whyte'); select last_name from customers where soundex(last_name) = soundex('bloo');
13.SUBSTR(x,start[,length])
--从x中获得,从start位置开始的,长度为length的子字符串 select substr(name,2,7) from products where product_id < 4; select substr(name,-5,3) from products where product_id < 4;
14.综合
select substr('Mary had a little lamb',12,6) from dual; select name,upper(substr(name,2,8)) from products where product_id < 4;
二、数值函数
1.ABS(x)
--返回x的绝对值 select abs(10),abs(-10) from dual; select product_id,price,price - 30,abs(price - 30) from products where product_id < 4;
2.CEIL(x)
--返回大于、等于x的最小整数(上取整) select ceil(5.8),ceil(-5.2) from dual;
3.FLOOR(x)
--返回小于、等于x的最大整数(下取整) select floor(5.8),floor(-5.2) from dual;
4.GREATEST(values)
--返回一组values中的最大值 select greatest(3,4,1) from dual; select greatest(50/2,exp(2)) from dual;
5.LEAST(values)
--返回一组values中的最小值 select least(3,4,1) from dual; select least(50/2,exp(2)) from dual;
6.POWER(x,y)
--返回x的y次幂 select power(2,1),power(2,3) from dual;
7.ROUND(x,[,y])
--返回x的四舍五入值,精确到小数点后y位 select round(5.75),round(5.75,1),round(5.75,-1) from dual;
8.SIGN(x)
--x为正,返回1;x为0,返回0;x为负,返回-1 select sign(-5),sign(5),sign(0) from dual;
9.SQRT(x)
--返回x的平方根 select sqrt(25),sqrt(5) from dual;
10.TRUNC(x[,y])
--返回x在小数点后y位的截断值 select trunc(5.75),trunc(5.75,1),trunc(5.75,-1) from dual;
三、转换函数
1.ASCIISTR(x)
--将x转换为ASCII字符串 select asciistr('ABC D CDE') from dual; select asciistr(chr(128) || ' ' || chr(129)||' '|| chr(130)) from dual;
2.BIN_TO_NUM(x)
--将二进制数x转换为NUMBER类型 select bin_to_num(1,0,1),bin_to_num(1,1,0),bin_to_num(1,1,1,0) from dual;
3.CAST(x AS type)
--将x转换为由type指定的兼容数据库类型 select cast(12345.67 as varchar2(10)), cast('9A4F' as raw(2)), cast('05-JUL-07' as date), cast(12345.678 as number(10,2)) from dual; select cast(price as varchar2(10)), cast(price +2 as number(7,2)), cast(price as binary_double) from products where product_id = 1;
4.CHARTOROWID(x)
--将x转换为rowid select rowid from customers where customer_id = 1; select * from customers where rowid = chartorowid('AAAWf4AAEAAAAHjAAA');
5.COMPOSE(x)
--将x转换为Unicode字符串 select compose('o'||unistr('