全部博文(11)
2011年(11)
分类: Oracle
2011-03-31 19:37:18
oracle 11g 007 sql 第3章 函数 笔记
SQL Functions (函数)
sql 函数分为以下:single-row(单行函数)、字符串函数、数字函数、日期函数;
函数分为:输入<——>输出,函数是一定有返回值的;
sql for two types of sql functions(分为2大类函数):单行函数和多行函数。
单行函数:主要做数据来的,每一行返回一个值,是可以嵌套(nested),可以是列的名字或表达式;
character functions 字符串函数
case-conversion 字符大小写函数分为:Lower、Upper、Intcap;
Lower 是输入不管是不是大小写,输出全部变小写;
Upper 是输入不管是不是大小写,输出全部变大写;
Initcap 输入不管是什么,输出头一个字符是大写;
Lower例子:
SQL> select lower('Hello') from dual;
LOWER('HELLO')
--------------
hello
Upper例子:
SQL> select upper('Hello') from dual;
UPPER('HELLO')
--------------
HELLO
Initcap例子:
SQL> select initcap ('HELLO') from dual;
INITCAP('HELLO')
----------------
Hello
注意:字符串是大小写敏感的。
character-manipulastion 字符串操作函数分为;concat、substp、length、instr、lpad、rpad、replace、trim
concat函数用法:
SQL> select concat('Hello','world') from dual; ---- concat('Hello','world')意思是'Hello','world'这两个字符串拼接起来;
CONCAT('HELLO','WORLD')
-----------------------
Helloworld
substp函数用法:
SQL> select substr('Helloworld',1,5) from dual; ----substr('Helloworld',1,5)意思是从'Helloworld'取第1个字符到第5个字符;
SUBSTR('HELLOWORLD',1,5)
------------------------
Hello
length函数用法:
SQL> select length('Helloworld') from dual; ----length('Helloworld')意思是算出'Helloworld'共几个字符;
LENGTH('HELLOWORLD')
--------------------
10
instr函数用法:
SQL> select instr('Helloworld','w') from dual; ----instr('Helloworld','w')意思是'Helloworld'的'w'是第几个字符;
INSTR('HELLOWORLD','W')
-----------------------
6
Lpad 函数用法:
SQL> select lpad('Helloworld',15,'*') from dual; ----lpad('Helloworld',15,'*')意思是共15个字符不够在左边添加成'*';
LPAD('HELLOWORLD',15,'*')
-------------------------
*****Helloworld
Rpad 函数用法:
SQL> select rpad('Helloworld',15,'*') from dual; ----rpad('Helloworld',15,'*')意思是共15个字符不够在右边添加成'*';
RPAD('HELLOWORLD',15,'*')
-------------------------
Helloworld*****
Replace 函数用法:
SQL> select replace('Jack and Jue','J','Bc') from dual; ----replace('Jack and Jue','J','Bc')意思是将'Jack and Jue'的'J'替换成'Bc';
REPLACE('JACKANDJUE','J','BC')
------------------------------
Bcack and Bcue
Trim 函数用法:
SQL> select trim('h' from 'helloworld') from dual; ----trim('h' from 'helloworld')意思是将'helloworld'的'h'的字符去掉;
TRIM('H'FROM'HELLOWORLD')
-------------------------
elloworld
第二节 number Functions 数字函数
数字函数分为:Round、Trunc、MOD;
Round 函数的用法:
SQL> select round(135.45,1) from dual; ----round(135.45,1)意思是将135.45保留一位小数四舍五入;
ROUND(135.45,1)
---------------
135.5
Trunc函数的用法:
SQL> select trunc(135.45,1) from dual; ----trunc(135.45,1)意思是将135.45的最后一位截断;
TRUNC(135.45,1)
---------------
135.4
MOD函数的用法:
SQL> select mod(3,4) from dual; ----mod(3,4)意思是将3除以4得到的余数是3;
MOD(3,4)
----------
3
将Round、trunc、mod函数联起来如下:
SQL> select round(45.923,2),trunc(45.923),mod(45.923,-1) from dual;
ROUND(45.923,2) TRUNC(45.923) MOD(45.923,-1)
--------------- ------------- --------------
45.92 45 0.923
以上就是字符串函数和数字函数的简单介绍,在以后的章节中还会介绍到更深的,大家只要了解一下意思就可以了。
日期函数 DD-MOD-RR
sysdate 函数返回当前的日期使用如下:
SQL> select sysdate from dual;
SYSDATE
-----------
2011/3/31 1
来个复杂的日期函数的换算使用如下:
SQL> select last_name,(sysdate-hire_date)/7 as weeks
2 from employees
3 where department_id=90;
LAST_NAME WEEKS
------------------------- ----------
King 1241.25863
Kochhar 1123.11577
De Haan 950.258634
以上的意思是求雇员进入公司有多少周了,但有小数我可以用Trunc函数来截断小数如下:
SQL> select last_name,trunc((sysdate-hire_date)/7,0) as weeke
2 from employees
3 where department_id=90;
LAST_NAME WEEKE
------------------------- ----------
King 1241
Kochhar 1123
De Haan 950