函数分为:
一.单值函数
1.字符函数
2.数字函数
3.日期函数
4.转换函数
二.分组函数(后面的章节再做学习)
dual //哑表
dual是一个虚拟表,用来构成select的语法规则,oracle保证dual里面永远只有一条记录。
1)字符函数
LOWER Converts to lowercase
UPPER Converts to uppercase
INITCAP Converts to initial capitalization
CONCAT Concatenates values
SUBSTR Returns substring
LENGTH Returns number of characters
NVL Converts a null value
• lower 把字符转为小写
例如:把'HELLO'转换为小写
select lower('HELLO')
from dual;
• upper 把字符转换为大写
例如:把'world'转换为大写
select upper('world')
from dual;
• initcap 把字符串首字母转换为大写
例如:把'hELLO'转换为首字母大写,其余字母小写
select initcap('hELLO')
from dual;
• concat 把俩个字符串连接在一起(类似之前的||的作用)
例如:把'hello'和'world'俩个字符串连接到一起,并且起个别名为msg
select concat('hello','world') msg
from dual;
• substr 截取字符串
例如:截取'hello'字符串,从第2个字符开始(包含第二个字符),截取后面连续的3个字符
select substr('hello',2,3)
from dual;
• length 获得字符串长度
例如:获得'world'字符串的长度
select length('world')
from dual;
• nvl 替换列中为null的值
select last_name,nvl(commission_pct,0)
from s_emp;
2)数字函数
ROUND Rounds value to specified decimal
TRUNC Truncates value to specified decimal
MOD Returns remainder of division
• round 四舍五入
round(arg1,arg2)
第一个参数表示要进行四舍五入操作的数字
第二个参数表示保留到哪一位
例如:
保留到小数点后面2位
select round(45.923,2)
from dual;
• trunc 截取到某一位
trunc(arg1,arg2)
和round的用法一样,但是trunc只舍去不进位
例如:
截取到小数点后面2位
select trunc(45.929,2)
from dual;
• mod 取余
mod(arg1,arg2)
第一个参数表示要进行取余操作的数字
第二个参数表示参数1和谁取余
例如:
把10和3进行取余 (10除以3然后获取余数)
select mod(10,3)
from dual;
3)日期函数
MONTHS_BETWEEN Number of months between two dates
ADD_MONTHS Add calendar months to date
NEXT_DAY Next day of the date specified
LAST_DAY Last day of the month
ROUND Round to date at midnight
TRUNC Remove time portion from date
sysdate关键字
表示系统的当前时间
例如:
显示时间:当前时间
select sysdate from dual;
注意:sysdate进行加减操作的时候,单位是天
例如:
显示时间:明天的这个时候
select sysdate+1 from dual;
• months_between 俩个日期之间相差多少个月(单位是月)
例如:
30天之后和现在相差多少个月
select months_between(sysdate+30,sysdate)
from dual;
• add_months 返回一个日期数据:表示一个时间点,往后推x月的日期
例如:
'01-2月-2016'往后推2个月
select add_months('01-2月-2016',2)
from dual;
注意:这个数字也可以是负数,表示往前推x月
• next_day 返回一个日期数据:表示一个时间点后的下一个星期几在哪一天
例如:
离当前时间最近的下一个星期5是哪一个天
select next_day(sysdate,'星期五')
from dual;
注意:
如果要使用'FRIDAY',那么需要把当前会话的语言环境修改为英文
• last_day 返回一个日期数据:表示一个日期所在月份的最后一天
例如:
当前日期所在月份的最后一天(月底)
select last_day(sysdate)
from dual;
• round 对日期进四舍五入,返回操作后的日期数据
例如:
把当前日期四舍五入到月
select round(sysdate,'MONTH')
from dual;
测试:15号16号分别是舍弃还是进位
(15号是舍弃,16号是进位)
//这个写法是错误的
//数字函数也有一个round
//俩个ronnd函数有冲突
//所以这里不能使用默认的日期格式
select round('01-2月-2016','MONTH')
from dual;
• trunc 对日期进行截取 和round类似,但是只舍弃不进位
4)类型转换函数
TO_CHAR converts a number or date string to a character string.
TO_NUMBER converts a character string containing digits to a number.
TO_DATE converts a character string of a date to a date value.
• to_char 把日期转换为字符
例如:
把当前日期按照指定格式转换为字符串
select to_char(sysdate,'yyyy')
from dual;
日期格式:
yyyy:四位数的年份
rrrr:四位数的年份
yy:两位数的年份
rr:两位数的年份
mm:两位数的月份(数字)
D:一周的星期几
DD:一月的第几天
DDD :一年的第几天
YEAR:英文的年份
MONTH:英文全称的月份
mon:英文简写的月份
ddsp:英文的第几天(一个月的)
ddspth:英文序列数的第几天(一个月的)
DAY:全英文的星期
DY:简写的英文星期
hh:小时
mi:分钟
ss:秒
• to_number 把字符转换为数字
例如:
select to_number('1000')
from dual;
//这个写法是错的 abc不能转换为数字
select to_number('abc')
from dual;
• to_date 把字符转换为日期
例如:
select to_date('10-12-2016','dd-mm-yyyy')
from dual;
oracle数据库中表示一个日期数据的几种方式
1.使用sysdate
2.使用oracle默认的日期格式 例如:'25-MAY-95'
3.使用日期函数ADD_MONTHS/NEXT_DAY/LAST_DAY/ROUND/TRUNC
4.使用转换函数to_date
函数之间的嵌套
格式:F3(F2(F1(arg0,arg1),arg2),arg3)
例如:
先把'hello'和'world'连接起来,再转换为字母大写然后再从第4个字符开始,连着截取4个字符
select substr(upper(concat('hello','world')),4,4)
from dual;