single row functions 单行函数
These functions operate on single rows only and return one result per row
接受一个或多个参数并为查询中得到的每一个数据行都返回一个结果
An argument can be one of the following:
- User-supplied constant
- Variable value
- Column name
- Expression
multiple row functions 多行函数 (
where 子句之后 只能跟单行函数)
对多数据行的群组进行操作,并且每组返回一个结果
字符函数
lower('SQL Course')----->sql course 返回小写
UPPER('sql course')----->SQL COURSE 返回大写
INITCAP('SQL Course')-----> Sql Course 返回首字母大写
CONCAT('good','string')---->good string 拼接 //只能拼接2个字符串
SUBSTR('String',1,3)---->str 截取
LENGTH('String')---->6 长度
Lpad(first_name,10,'* ')右填充
Rpad(first_name,10,' *')左填充
ROUND(45.923,2)---->45.92 四舍五入
TRUNC(45.923)---->45 不入只截
to_number('1550') 显式数据类
型转换
to_char(salary,'$99,999.99') 数字转字符
TRIM函数
1. trim()删除字符串两边的空格。
2. ltrim()删除字符串左边的空格。
3. rtrim()删除字符串右边的空格。 //select rtrim( ' ssmitss ')||'ok' from dual
4. trim('字符1' from '字符串2') 分别从字符2串的两边开始,删除指定的字符1。
5. trim([leading | trailing | both] trim_char from string) 从字符串String中删除指定的字符trim_char。
leading:从字符串的头开始删除。
trailing:从字符串的尾部开始删除。 //select trim(trailing 's' from 'ssmitss') from dual
borth:从字符串的两边删除。
6. tim()只能删除半角空格。
数字函数
round 对指定的值
做四舍五入,可对日期类型操作
trunc对指定的值取整
select ename, hiredate, trunc(hiredate,'YEAR')
from emp //对日期也可取整
mod 返回除法后的余数
日期函数
Oracle Date Format
Oracle stores
dates in an internal numeric format,
(因为日期在oracle里是以数字形式存储的,所以可对它进行加减运算,计算是以天为单位。)
representing the century, year, month, day, hours, minutes, and seconds.
The default display and input format for any date is
DD-MON-YY.
Valid Oracle dates are between January 1, (公元前)4712 , and December 31, (公元)9999
时间格式
select to_date('2003-11-04 00:00:00' ,'YYYY-MM-DD HH24:Mi:ss')
FROM dual
MONTHS_BETWEEN //计算两个日期之间的月数
select months_between('1994-04-01','1992-04-01') mm from dual
ADD_MONTHS //给日期增加月份
select add_months('1992-03-01',4) am from dual
NEXT_DAY //给定日期的下一个指定日期(下周几)
select next_day(sysdate, 'friday') n_fd from dual
LAST_DAY //日期当前月份的最后一天
select last_day('1989-03-28') l_d from dual
ROUND四舍五入,TRUNC截断
SELECT empno, hiredate,
round(hiredate,'MONTH') AS round,
trunc(hiredate,'MONTH') AS trunc
FROM emp
WHERE empno=7788
SELECT empno, hiredate,
round(hiredate,'YEAR') AS round,
trunc(hiredate,'YEAR') AS trunc
FROM emp
WHERE empno=7839
数据类型转换
TO_CHAR
日期到字符
select ename, hiredate, to_char(hiredate, 'MM/YY') month_hired
from emp
where ename='SCOTT'
fm压缩空格或左边的'0'
to_char(hiredate, 'fmMM/YY')
to_char(hiredate,'fmDD MM YYYY')
数字到字符:
9表示数字,$本地化货币字符
select ename, to_char(sal, '$99,999.99') Salary
from emp
where ename='SCOTT'
TO_NUMBER
SELECT to_number('$123.45','$999.99') "result" FROM dual
TODATE
select to_date('1983-11-12', 'YYYY-MM-DD') tmp_DATE from dual
chr()函数将ASCII码转换为字符:
ASCII码 –》 字符
ascii()函数将字符转换为ASCII码:字符 –》 ASCII码
在oracle中chr()函数和ascii()是一对反函数。
求字符对应的ASCII值
select ASCII('A') FROM dual
decode函数用法:
SELECT job, sal,
DECODE(job, 'ANALYST', SAL*1.1,
'CLERK', SAL*1.15,
'MANAGER', SAL*1.20,
SAL)
REVISED_SALARY
FROM emp
/
SELECT ename, sal,
DECODE(TRUNC(sal/1000, 0),
0, 0.00,
1, 0.09,
2, 0.20,
3, 0.30,
4, 0.40,
5, 0.42,
6, 0.44,
0.45) TAX_RATE
FROM emp
WHERE deptno = 30