【sql语句】实验三 SQL*Plus 中常用函数

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40307434/article/details/84332116
save C:\Users\DH2016PSY\Desktop\数据库PPT18\SY3\SY3.sql;
save C:\Users\DH2016PSY\Desktop\数据库PPT18\SY3\SY3.sql append;

1.ASCII:返回与指定的字符对应的ASCII码。

select ascii('A') A,ascii('a') a,ascii('0') zero,ascii(' ') space from dual; 

2.CONCAT:连接两个字符串;

select concat('010-',' 888888')||'转23' tel from dual;

3.INITCAP:返回字符串并将字符串的第一个字母变为大写;

select initcap('smith') upfirst from dual;

4.INSTR(C1,C2,I,J):在一个字符串中搜索指定的字符,返回发现位置(no:0);
C1:被搜索的字符串
C2:希望搜索的字符串
I:搜索的开始位置,默认为1
J:出现的位置,默认为1

select instr('hello world ','ello',1,1) instring from dual;

5.LOWER:返回字符串,并将所有的字符小写

select lower('AAABBbbCC')sm from dual;

6.UPPER:返回字符串,并将所有的字符大写

select upper('AAABBbbCC')up from dual;

7.RPAD和LPAD(粘贴字符):RPAD在列的右边补字符,LPAD在列的左边补字符(小于则删除原有字符)

select lpad(rpad('ABCDE',4,'r'),20,'l') pad from dual;

8.LTRIM和RTRIM:LTRIM 删除左边出现的字符串

select ltrim(rtrim('      wow      ',' '),' ') del_blank from dual;

9.SUBSTR(string,start,count)取子字符串,从start开始,取count个

select substr('1234567',1,3)sub from dual;//从0开始

10.REPLACE(‘string’,‘s1’,‘s2’)//在string中所有出现的s1均换为s2

select replace ('she love you','she','i')qh from dual;//AMAZING!!!!

11.TRIM(‘s’ from ‘string’),LEADING剪掉前面的字符,TRAILING剪掉后面的字符,如果不指定,默认为空格符//这句至今不懂怎么用啊

select trim('s'from 'string') trim from dual;

12.ABS:返回指定值的绝对值

select abs(-1e8),abs(-99.9) from dual;

13.EXP:返回一个数字e的n次方根

select exp(2),exp(1) from dual;

14.FLOOR:对给定的数字取整数

select floor(2345.67) from dual;//2345

15.MOD(n1,n2):返回一个n1除以n2的余数

select mod(10,4),mod(10.22,4) from dual;//2 2.22

16.POWER:返回n1的n2次方根

select power(2,10), power(4,0.5) from dual;

17.ROUND(四舍五入)和TRUNC(只舍)按照指定的精度进行舍入

select round(55.55),round(-55.4),trunc(55.5),trunc(-55.5) from dual;//56 -55 55 -55

18.SIGN取数字n的符号,大于0返回1,小于0返回-1,等于0返回0

select sign(12),sign(-11),sign(0.00) from dual;//1 -1 0

19.SQRT返回数字n的根

select sqrt(64),sqrt(1.21)from dual;//8 1.1

20.TRUNC:按照指定的精度截取一个数

select trunc(123.344,-2), trunc(12.3444,2)from dual;// 100 12.34

21.ADD_MONTHS增加或减去月份

select to_char(add_months(to_date('199912','yyyymm'),2),'yyyymm')from dual;// 200002

22.LAST_DAY返回日期月份对应的最后一天

select to_char(last_day(sysdate),'yyyy.mm.dd')last_day from dual;//2018.11.30

23.MONTHS_BETWEEN(date2,date1)给出date2-date1的月份

select months_between(to_date('20180521','yyyymmdd'),sysdate)delta from dual;

24.NEXT_DAY(date,‘day’)给出日期date和星期x之后计算下一个星期的日期

select to_char(next_day(sysdate,'星期日'), 'yyyy-mm-dd')next_day from dual;

25.SYSDATE:用来得到系统的当前日期

select to_char(sysdate,'yyyy/mm/dd')now from dual;
trunc(date,fmt)按照给出的要求将日期截断,如果fmt='mi'表示保留分,截断秒
select to_char(trunc(sysdate,'hh'),'yyyy.mm.dd hh24:mi:ss') h1,
	to_char(trunc(sysdate,'mi'),'yyyy.mm.dd hh24:mi:ss') h2 from dual;
//2018.11.21 22:00:00 2018.11.21 22:47:00

26.TO_DATE(string,‘format’)将字符串转化为ORACLE中的一个日期

select to_date('19980924','yyyymmdd')birth from dual;// 24-9月 -98

27.TO_NUMBER:将给出的字符转换为数字

select to_number('1999')year from dual;

28.LEAST:返回一组表达式中的最小值

select least('啊','安','天') from dual;//啊

29.UID返回标识当前用户的唯一整数

show user// USER 为 "SCOTT"
用sys用户登录授权sysdba给system//sys密码:system密码 as sysdba
grant sysdba to system
grant select[update,delete,insert] on dba_users to scott;//system授权,授权不足用dba
select username,user_id from dba_users where user_id=uid;//DBA权限

30.USER返回当前用户的名字

select user from  dual;

31.MAX(DISTINCT|ALL)求最大值,ALL表示对所有值求最大值,DISTINCT不同值

select max(distinct sal) from emp;

32.MIN(DISTINCT|ALL)求最小值

select min(all sal) from emp;

33.STDDEV(distinct|all)求标准差,//默认all

select stddev(sal) from emp;// 1182.50322
select stddev(distinct sal) from emp;//  1229.95096

34.VARIANCE(DISTINCT|ALL) 求协方差

select variance(sal) from emp;

猜你喜欢

转载自blog.csdn.net/qq_40307434/article/details/84332116