oracle-内置函数

Day 02	下标从1开始

select substr(phone,1,3) || '****' || substr(phone,8) from student;

后面有,前面可有可没有。后面没有,前面不能有,主函数除外

在环境变量 新建一个名字为:nls_date_format
	内容:yyyy-mm-dd hh24:mi:ss
--------------------------------------------------------------
运算符:
算术:
	+ - * / mod(x,y)取余
	select mod(14,5) from dual;						//取余:14/5

关系比较:
	>  <  >=  <=  =  !=
	******<>*******不等于(标准的)

条件:
	and                         表示:并且   同时成立
	or                            表示:或者   有一个条件成立就可以
	between and          表示:闭合区间

与空判断:
	is null          表示:空
	is not null   表示:不空
**********************************************************************
多行函数:组函数:聚簇函数:聚组函数
	count()  记录数    sum()  求和    avg()  平均值   max()  最大值  min()  最小值     

1.查看当前表有多少条记录
	select count(salary) from student;--针对某一字段
	select count(*) from student;--针对所有记录的所有字段
2.统计某个字段的和
	select sum(salary) from student;
3.求平均数
	select avg(salary) from student;
4.求最大值
	select max(salary) from student;
5.求最小值
	select min(salary) from student;
**********************************************************************
单行函数:		//都需要dual测试表
	ceil():返回大于等于x的最小整数
		 select ceil(11.2-2.6) from dual;	//结果:9   取出大于等于括号的最小整数
	floor():返回小于等于x的最大整数
		select floor(11.9-2) from dual;		//结果 :9    取出小于等于括号的最大整数

	round():四舍五入
	round(a1,a2) 保留指定小数位的四舍五入
		select round(3.141592653,3) from dual;

	trunc():直接截断,后面的不要了
	trunc(a1,a2) 保留指定位数的小数
		select trunc(3.141592653,3) from dual;

	sign():求符号位  正数:1   负数:-1  零返回0
		select sign(-888) from dual;

	abs():求绝对值
		select abs(-888) from dual;

	power(a,b):求a的b次方
		select power(2,3) from dual;

	sqrt():求正平方根
		select sqrt(16) from dual;
**********************************************************************
sqlplus中当前会话日期输出格式修改方法
alter session set nls_date_format = 'yyyy-mm-dd hh24:mi:ss';
永久修改方法(详见环境变量)在环境变量 新建一个名字为:nls_date_format
				内容:yyyy-mm-dd hh24:mi:ss
**********************************************************************
日期函数:
日期可以加减(整数)运算    单位是:天  相差结果多少天
两个日子没法相加

yyyy   年   year      年
mm    月   month  带‘月’的月份
ddd    年中的天
dd      月中的天
d        周中的天
hh      12小时制
hh24  24小时制
mi      分钟
ss       秒
xff      毫秒
ff3      毫秒保留3位
**********************************************************************
java:yyyy-MM-dd HH:mm:ss
Oracle:yyyy-mm-dd hh24:mi:ss:xff	//不写24默认12
           yyyy-mm-dd hh24:mi:ss:ff3
**********************************************************************
add_months(c1,c2):在某个日子上增加多少个月
	c1:日期
	c2:整数值
	select add_months(sysdate,10) from dual;

months_between(c1,c2):计算两个日期之间的月份
	计算方式:c1-c2
	select months_between(sysdate,'2018-03-17') from dual;

last_day(c1):计算给定日期所在月份的最后一天		//(last_day(hiredate)-2);
	select last_day(sysdate) from dual;

next_day(c1,c2)
	c1:日期
	c2:周中的某天
	select next_day(sysdate,'星期三') from dual;
=========================================
转换函数:	//select * from 表名
to_number():将一个字符类型的数字变成数值类型
	select * from student where name = 'xiaobai' and to_number(email) =5;	//字符型到数字型

to_char():	
	1.将数字转换成字符串
	select * from student where '234.56' = to_char(salary);
	2.常用在货币单位,格式化字符串
	select to_char(123123123123123.00,'999,999,999,999,999.00') from dual;	//数字转成字符串
				//只能是999,999,999
***********************************************************************************
3.日期转换 	//select to_char()/date()  	//还有dual万能表
to_char(日期,'yyyy-mm-dd hh24:mi:ss')						//单独取出年,月,日,拿到自己想要的年月日
	select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;			to_char(sysdate,'yyyy')
				//不能有ff3
	select to_char(systimestamp,'yyyy-mm-dd hh24:mi:ss:ff3') from dual;
					//或者ssxff
to_date(c1,c2)								//把年月日,转成需要的格式
	c1:字符类型的日期						to_date('20180304','yyyy-mm-dd')
	c2:格式
	select to_date('20180304','yyyy-mm-dd') from dual;	//没有时分秒
	select to_date('20180304142334','yyyy-mm--dd hh24:mi:ss') from dual;//有时分秒

取出年 取出月 取出日
select to_char(to_date('20180717','yyyy-mm-dd'),'yyyy') from dual;
select to_char(to_date('20180717','yyyy-mm-dd'),'mm') from dual;
select to_char(to_date('20180717','yyyy-mm-dd'),'dd') from dual;
*************************************************************************************
字符函数:		//from dual;万能表
lower():转换成小写
upper():转换成大写
initcap():首字母大写
length():长度
select upper(name),initcap(name),length(name) from student where name = 'xiaobai'; 	//看这个,都有参数
******************************************************
substr(c1,c2,c3): 截取字符串(第三个参数不写,默认到最后)
	c1:被截取的字符串
	c2:从哪个位置开始截取
	c3:截取长度  默认截取到最后				//在Oracle里最后一个参基本上是截取长度
	select substr('woshizhizhuxia',6) from dual;
	select substr('woshizhizhuxia',6,3) from dual;
查询手机号中间四位以****代替
	select substr(phone,1,3) || '****' || substr(phone,8) from student;		
						//第三个参数不写,默认到结束

instr(c1,c2,c3,c4):索引字符串		//要么写两个,要么写四个,方便
	c1:被索引的字符串
	c2:希望找到的字符
	c3:从哪个位置开始找  默认是1
	c4:第几次出现
	select instr('woshizhizhuxia','zh',6,2) from dual;
******************************************************
concat(c1,c2):拼接字符串
	select concat('123','456') from dual;
	select '123' || '456' from dual;

lpad(c1,c2,c3):左侧补全
rpad(c1,c2,c3):右侧补全
	c1:希望补全的字符串
	c2:补全到多少位
	c3:用哪个字符来补
	select rpad('abc',10,'d') from dual;

trim(c1):默认把c1两侧去除空格
	trim(c1 from c2):把c2的两侧移除指定的c1
	select trim('a' from 'aaaaaaaaabdaaddaaaaaa') from dual;

ltrim(c1,c2):左侧去除
rtrim(c1,c2):右侧去除
	c1:被去除的字符串
	c2:去除的字符串  默认是空格
	select ltrim('           abc',' ') from dual;

replace(c1,c2,c3):完全替换		      //replace(1,2,3) 含有就替换,不用全部一样
	c1:原字符串	
	c2:被替换的字符串
	c3:替换的字符串
	select replace(name,'x','3') from student;		//from student;
******************************************************
通用函数
nvl():空值处理				//nvl空值处理,把空值替换为888
nvl(字段,替换显示的内容)
	select name,email,nvl(email,'888') from student;	//是空换成888

nvl2():空值处理二代				
nvl2(字段,不是空显示什么,是空显示什么)
	select name,email,nvl2(email,'888','666') from student;
						//nvl2空值处理,把不空值的替换为888,空的替换为666

******************************************************
decode(c1,c2,c3,c4,c5,...Cx,Cx+1)
c1:拿来判断的值			//奇偶数算上c1
从第二参数开始,每两个参数看作是一组,拿每一组的第一个参数和c1进行比较
如果相同则返回该组的第二个参数
第一次判断:c2==c1?c3:
第二次判断:c4==c1?c5:
如果参数个数是奇数个,并且最终判断没有相同的数据,则返回空
如果参数个数是偶数个,并且最终判断没有相同的数据,则返回最后一个参数的值
					//奇偶数算上c1
select name,decode(phone,'3',salary+100,'5',salary-100,'7',salary+200,salary+500)a from student;

条件取值语句:
select name,phone,		--展示的标题栏(展示的栏,写在前在前面,注意有逗号)
(case phone	         	--开始判断的条件(每次都要比的)
when '3' then salary + 100	--当条件成立就..... (注意没有逗号)
when '5' then salary - 100
when '7' then salary + 200
else salary + 500		--默认值,如果都没有相同的,就会返回默认值
end)a			--结束(a代表这些代码用a表示,更简洁)
from student;		--查哪张表
============================================
排序  order by					//写在by后面,和group by 一样
	排序字段:desc 代表降序       asc  代表升序   默认升序
order by 后面加多个字段
order by 字段1 desc,字段2 asc
select * from student order by salary desc;
select * from student order by salary desc,phone asc;
				//可以写多个一起排序,参数在前,先按照排序
分组 group by
	根据某一个或者多个列上的值,将该列上相同的值所在的记录划分为一个组
	这样一张表就可以被划分为多个组
*******如果以字段A分组,那么只能查询A,或者用组函数统计其他字段,不能直接查询其他字段****
***************后面有,前面可有可没有。后面没有,前面不能有,主函数除外*************
select phone,count(salary) from student group by phone;
           后面有   组函数统计除外    		呼应*****************************
		phone相同的,划分为一个组(按照salary)
		括号里的代表:相同的有几条
		呼应的代表:关注什么数据
having 条件
select phone,count(salary) from student group by phone having phone = 6;
            呼应   组函数统计除外  				         呼应
		phone=6的数据,划分为一个组(按照salary)
				//having name=‘chi’ 只看这一栏的条数
				//having 只能出现在group by 之后,无group by 无having
wm_concat 展示组内在某个列上有哪些数据
		//一列数据,放在一行,中间逗号隔开
效果
	a		
	b	=>	a,b,c      
	c
================================================
查询语句常见关键字的优先级:
from      	表名     --from 优先级最高
where	条件      --优先级次高
group by	分组     --优先级次于where
having  	条件     --优先级一定在group by之后
select   	列名     --优先级高于order by
order by	排序     --优先级最低

查询什么 select 什么
输出什么 条件些什么

Oracle内置的系统函数
	让我们一起来感受Oracle和Java的区别
		str.substring(1,2); 参数代表下标
		substr(str,1,2); 参数代表第几个字
字符处理函数:
	字符函数:
		length() : 得到字符的长度(字符)
		lengthb() : 得到字符的长度(字节)
		lower() : 将内容全部转换成小写
		upper() : 将内容全部转换成大写
		initcap() : 每个单词的首字母大写
		substr(c1,c2,c3): 截取字符串*(重载哦)(第三个参数不写,默认到最后)
			c1:被截取的字符串
			c2:从哪个位置开始截取
			c3:截取长度  默认截取到最后
			select substr(phone,1,3) || '****' || substr(phone,8) from student;
		instr(c1,c2,c3,c4):索引字符串		
			c1:被索引的字符串
			c2:希望找到的字符
			c3:从哪个位置开始找  默认是1
			c4:找第几次出现的位置
		replace(c1,c2,c3):完全替换		    
			c1:原字符串	
			c2:被替换的字符
			c3:替换的字符
		lpad(c1,c2,c3):左侧补全
		rpad(c1,c2,c3):右侧补全
			c1:希望补全的字符串
			c2:补全到多少位
			c3:用哪个字符来补
		ltrim(c1,c2):左侧去除
		rtrim(c1,c2):右侧去除
			c1:被去除的字符串
			c2:去除的字符串 ,默认是空格
		trim(c1):默认把c1两侧去除空格
		concat(c1,c2):拼接字符串 :
			 concat(1,2) ==> 1||2
		
单行函数:
	数字函数:ceil(x) : 返回>=指定数值x的最小整数
		  floor(x) : 返回<=指定数值x的最大整数
		  trunc(a1,a2): 截断(要处理的数值,位数),直接截断,后面的不要了
		  round() : 四舍五入(要处理的数值,位数)
		  =====================================
		  abs() : 绝对值
		 power(a,b):求a的b次方
		  sqrt(x) : 求x正平方根
		sign():求符号位      正数:返回1      负数:返回-1      零:返回0
日期函数
	日期函数:sysdate  systimestamp  
		add_months(c1,c2):在某个日子上增加多少个月(可以是负数哦~)
			c1:日期
			c2:整数值
		  last_day(c1) : 得到指定日期c1那个月的最后一天
		  next_day(c1,c2) : 得到指定日期的下一个周几 next_day(date,'星期日')
			c1:日期
			c2:周中的某天
		  months_between(c1,c2) : 计算两个日期之间相差多少个月	
			计算方式:c1-c2
日期转换,字符数值转换:		  
	转换函数:to_number()   to_char()  to_date()
		to_number(c1):将一个字符类型的数字变成数值类型:'xiaobai' and to_number(email) =5
		to_char():	
			1.将数字转换成字符串:to_char(salary)
			2.常用在货币单位,格式化字符串:123123123123123.00,'999,999,999,999,999.00'
			3.单独取出年,月,日: to_char(sysdate,'yyyy-mm-dd hh24:mi:ss')
		to_date(c1,c2):把时间戳年月日转成需要的格式:to_date('20180304','yyyy-mm-dd')
		          c1:字符类型的日期	
		          c2:格式
		to_char(to_date())的合用:
			select to_char(to_date('20180717','yyyy-mm-dd'),'dd') from dual;
空值处理:
	通用函数
						
		nvl(字段,替换显示的内容)
			select name,email,nvl(email,'888') from student;	//nvl空值处理,把空值替换为888			
		nvl2(字段,不是空显示什么,是空显示什么)
				select name,email,nvl2(email,'888','666') from student;
						//nvl2空值处理,把不空值的替换为888,空的替换为666
发布了89 篇原创文章 · 获赞 32 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_41345773/article/details/102806407