Oracle 基础语句 函数大全(字符串函数,数学函数,日期函数,逻辑运算函数...
oracle中基本函数

(一)字符函数UpperSELECT Upper ('abcde') FROM dual 结果:ABCDELowerSELECT lower('ABCDE') FROM dual 结果:abcdeInitcapSELECT Initcap ('AAA') FROM dual 结果:AaaSELECT Initcap ('aaa') FROM dual 结果:AaaConcatSELECT Concat ('a', 'b') FROM dual 结果:abSelect 'a' || 'b' from dual 结果:abSubstrSelect substr('abcde',0,3) from dual 结果:abcSelect substr('abcde',-2,3) from dual 结果:de第二个参数为字符串的第几位,默认从0开始。
当为负数时,是从右侧开始。
第三个参数为要取得字符串的总长度。
LengthSelect length('abcde') from dual 结果:5ReplaceSelect replace('abcde','a','A') from dual 结果:AbcdeInstr返回指定一字符串在另一字符串中最先出现的位置。
在字符串string1中,从start开始找string2,省略start时从string1头开始找。
找不到时,函数值为0。
INSTR(char1,char2,start,times):在char1字符串中搜索char2字符串,start为执行搜索的起始位置,times为搜索次数。
InStr([start, ]string1, string2[, compare])Select instr('Hello World','W') from dual 结果:8Select instr('Hello World','w') from dual 结果:0如果在第一个参数中存在第二个参数,则返回第一个遇到的匹配参数的位置,该方法区分大小写。
oracle常用函数汇总(分享)

oracle常⽤函数汇总(分享)算术运算符:+ - * / 可以在select 语句中使⽤连接运算符:|| select deptno|| dname from dept;⽐较运算符:> >= = != < <= like between is null in逻辑运算符:not and or集合运算符: intersect ,union, union all, minus对应集合的列数和数据类型相同查询中不能包含long 列列的标签是第⼀个集合的标签使⽤order by时,必须使⽤位置序号,不能使⽤列名例:集合运算符的使⽤:复制代码代码如下:intersect ,union, union all, minusselect * from emp intersect select * from emp where deptno=10 ;select * from emp minus select * from emp where deptno=10;select * from emp where deptno=10 union select * from emp where deptno in (10,20); --不包括重复⾏select * from emp where deptno=10 union all select * from emp where deptno in (10,20); --包括重复⾏ TO_DATE格式(以时间:2007-11-02 13:45:25为例)Year:yy two digits 两位年显⽰值:07yyy three digits 三位年显⽰值:007yyyy four digits 四位年显⽰值:2007Month:mm number 两位⽉显⽰值:11mon abbreviated 字符集表⽰显⽰值:11⽉,若是英⽂版,显⽰novmonth spelled out 字符集表⽰显⽰值:11⽉,若是英⽂版,显⽰novemberDay:dd number 当⽉第⼏天显⽰值:02ddd number 当年第⼏天显⽰值:02dy abbreviated 当周第⼏天简写显⽰值:星期五,若是英⽂版,显⽰friday spelled out 当周第⼏天全写显⽰值:星期五,若是英⽂版,显⽰fridayddspth spelled out, ordinal twelfthHour:hh two digits 12⼩时进制显⽰值:01hh24 two digits 24⼩时进制显⽰值:13Minute:mi two digits 60进制显⽰值:45Second:ss two digits 60进制显⽰值:25其它Q digit 季度显⽰值:4WW digit 当年第⼏周显⽰值:44W digit 当⽉第⼏周显⽰值:124⼩时格式下时间范围为: 0:00:00 - 23:59:59....12⼩时格式下时间范围为: 1:00:00 - 12:59:59 ....select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as nowTime from dual; //⽇期转化为字符串select to_char(sysdate,'yyyy') as nowYear from dual; //获取时间的年select to_char(sysdate,'mm') as nowMonth from dual; //获取时间的⽉select to_char(sysdate,'dd') as nowDay from dual; //获取时间的⽇select to_char(sysdate,'hh24') as nowHour from dual; //获取时间的时select to_char(sysdate,'mi') as nowMinute from dual; //获取时间的分select to_char(sysdate,'ss') as nowSecond from dual; //获取时间的秒select to_date('2004-05-07 13:23:44','yyyy-mm-dd hh24:mi:ss') from dual//显⽰Two Hundred Twenty-Twoselect to_char(to_date('2002-08-26','yyyy-mm-dd'),'day') from dual;星期⼀select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual; monday设置⽇期语⾔ALTER SESSION SET NLS_DATE_LANGUAGE='AMERICAN';也可以这样TO_DATE ('2002-08-26', 'YYYY-mm-dd', 'NLS_DATE_LANGUAGE = American')select floor(sysdate - to_date('20020405','yyyymmdd')) from dual;select id, active_date from table1UNIONselect 1, TO_DATE(null) from dual;注意要⽤TO_DATE(null)a_date between to_date('20011201','yyyymmdd') and to_date('20011231','yyyymmdd')那么12⽉31号中午12点之后和12⽉1号的12点之前是不包含在这个范围之内的。
oralce函数

oralce函数Oracle是一种关系数据库管理系统,它使用了一种名为Oracle数据库的数据库管理系统。
Oracle是一种强大的工具,提供了许多内置函数,可以用于在数据库中进行各种操作。
以下是一些常用的Oracle函数。
1.聚合函数-AVG:计算指定列的平均值。
-COUNT:计算指定列中非空数据的数量。
-SUM:计算指定列的总和。
-MAX:找到指定列的最大值。
-MIN:找到指定列的最小值。
2.字符串函数-CONCAT:将两个字符串连接成一个字符串。
-LOWER:将字符串转换为小写。
-UPPER:将字符串转换为大写。
-LENGTH:计算字符串的长度。
-SUBSTR:返回一个字符串的子字符串。
3.数值函数-ROUND:将一个数值四舍五入到指定的小数位数。
-CEIL:向上取整,返回不小于指定数值的最小整数。
-FLOOR:向下取整,返回不大于指定数值的最大整数。
-ABS:返回指定数值的绝对值。
-MOD:返回两个数值的余数。
4.日期和时间函数-SYSDATE:返回当前日期和时间。
-ADD_MONTHS:在指定日期上增加指定的月份。
-TRUNC:截断日期或时间到指定的精度。
-MONTHS_BETWEEN:计算两个日期之间的月数差。
-TO_CHAR:将日期转换为指定格式的字符串。
5.条件函数-DECODE:根据条件返回不同的值。
-CASE:根据条件执行不同的操作。
-NVL:如果给定的表达式为NULL,则将其替换为指定的值。
-NULLIF:如果两个表达式的值相等,则返回NULL。
6.分析函数-ROW_NUMBER:为每一行分配一个唯一的数字。
-RANK:为每一行分配一个排名,如果有并列的值,则排名相同。
-DENSE_RANK:为每一行分配一个排名,如果有并列的值,则排名可以重复。
-LEAD:返回指定行后的值。
-LAG:返回指定行前的值。
上述函数只是Oracle提供的一小部分功能,Oracle还提供了许多其他有用的函数。
Oracle 数据库常用函数

pl/sql中的case语句select initcap('dfdsj') from dual;select replace('ddff','f','') from dual;select translate('ddff','f','d') from dual;select substr('asdfjkl',1,4) from dual;select current_date from dual;select sysdate from dual;select concat('010-','88888888')||'转23 高乾竞电话' from dualselect extract(year from sysdate) from dual;select instr('oracle traning','ra',1,2) instring from dual;select lpad(rpad('gao',10,'*'),17,'*')from dual;select rpad('gao',10,'*') from dual;select replace('1,23,333,000.00',',','') jine from dual;select trim(' ddd ') from dual;select to_char(sysdate,'yyyy.mm.dd'),to_char((sysdate)+1,'yyyy.mm.dd') frselect to_date('2010-01-11', 'YYYY-MM-DD') from dual;select months_between('19-12月-1999','19-3月-1999') mon_between from dual;select to_char(trunc(sysdate,'hh'),'yyyy.mm.dd hh24:mi:ss') hh,to_char(trunc(sysdate,'mi'),'yyyy.mm.dd hh24:mi:ss') hhmm from duaselect user from dual;select deptno,count(*),sum(sal) from scott.emp group by deptno; select decode(1 , 1 , 'x is 1', 2 , 'x is 2', 'others') from dual;字符串首个字符大写将字符串中出现的f替换为空,即将f删除截取字符串从第1-4位即asdf提取时间的年月日,时分秒的提取非这种写法查找字符串出现的位置左粘帖右粘帖将字符串中出现的‘,’替换为空,即将‘,’清除清除左右出现的空格日期转换为字符字符转换为日期形式een from dual;hh,to_char(trunc(sysdate,'mi'),'yyyy.mm.dd hh24:mi:ss') hhmm from dual;类似于case语句,即输出为‘x is 1’。
Oracle之常用函数

Oracle之常⽤函数 SQL语句根据参数的不同,分为单⾏函数 和 多⾏函数。 [1] 单⾏函数:输⼊是⼀⾏,输出也是⼀⾏; [2] 多⾏函数:输⼊多⾏数据,输出⼀个结果。 在执⾏时,单⾏函数是检索⼀⾏处理⼀次,⽽多⾏函数是将检索出来的数据分成组后再进⾏处理。 根据函数参数不同,SQL函数⼜分为数值函数、字符函数、⽇期函数、转换函数、聚集函数等多种。1、数值函数:函数的输⼊、输出都是数值型数据 单⾏数字函数操作数字数据,执⾏数学和算术运算。所有函数都有数字参数并返回数字值。所有三⾓函数的操作数和值都是弧度⽽不是⾓度,oracle没有提供内建的弧度和⾓度的转换函数。
1.1 ABS(n) :返回n的绝对值3.2 ACOS(n) :反余⽞函数,返回-1到1之间的数。n表⽰弧度 select ACOS(-1) pi,ACOS(1) ZERO FROM dualPI ZERO3.14159265 03.3 ASIN(n) :反正⽞函数,返回-1到1,n表⽰弧度3.4 ATAN(n) :反正切函数,返回n的反正切值,n表⽰弧度。3.5 CEIL(n) :返回⼤于或等于n的最⼩整数。3.6 COS(n) :返回n的余⽞值,n为弧度3.7 COSH(n) :返回n的双曲余⽞值,n 为数字。 select COSH(<1.4>) FROM dualCOSH(1.4)2.150898473.8 EXP(n) :返回e的n次幂,e=2.71828183.3.9 FLOOR(n) :返回⼩于等于n的最⼤整数。3.10 LN(n) :返回以e为底,n的对数,n必须⼤于03.11 LOG(m, n): 返回以m为底n的对数3.12 MOD(m, n) :返回m除以n的余数3.13 POWER(m,n): 返回m的n次⽅3.14 ROUND(m [, n]) 对m进⾏四舍五⼊(当n⼤于0时,将m四舍五⼊到⼩数点右边n位;当n省略时,对m取整;当n⼩于0时,将m四舍五⼊到⼩数点左边n位;n2必须是整数)。
ORACLE常用数值函数、转换函数、字符串函数介绍.docx

Concat (stl, st2) returns st to st2 to the end of stl (the
available operator 〃| | 〃)
The lpad (stl, n [, st2]) returns the right-aligned st, which is filled to the left of stl with st2 filling up until the length is n, and the default for st2 is space
' 『,'a', '
Select substr (' Archibald bearisol ', 6, 9) a, substr (' Archibald bearisol 11) Bald bear bearisol
Select name, instr (name, ') a, instr (name, ', ', 1, 2) b from
ORACLE USES numeric functions, transformation functions, and string functions ? Numerical function: The absolute value of abs (m) m Mod m (n) m is the remainder of n Power (m, n) m to the n Round (m [n]) m four round the decimal point (n default is 0). Trunc (m [n]) m intercepts the value of n bits (n defaults to 0)
oracle 函数的用法
oracle 函数的用法Oracle 是一个非常强大的数据库管理系统,它提供了丰富的函数库,可以让我们在SQL 语句中使用各种函数来实现数据处理、计算和查询。
本文将给大家介绍一些常用的Oracle 函数及其用法。
一、字符串函数1. UPPER 函数:将字符串转换为大写字母。
SELECT UPPER('hello world!') FROM DUAL;结果为:HELLO WORLD!结果为:hello world!3. INSTR 函数:查找子字符串在字符串中第一次出现的位置。
结果为:54. SUBSTR 函数:截取字符串的一部分。
5. REPLACE 函数:将字符串中的某个子串替换为另一个子串。
二、数值函数1. ROUND 函数:将指定的数字四舍五入到指定的小数位数。
3. ABS 函数:计算数字的绝对值。
4. SIGN 函数:返回数字的符号,如果为正数返回 1,为负数返回 -1,为零返回 0。
SELECT SIGN(-10), SIGN(10), SIGN(0) FROM DUAL;结果为:-1 1 05. POWER 函数:计算一个数的指定次幂。
三、日期函数1. SYSDATE 函数:返回当前系统日期和时间。
SELECT SYSDATE FROM DUAL;2. MONTHS_BETWEEN 函数:计算两个日期之间相差的月数。
SELECT MONTHS_BETWEEN('2022-01-01', '2021-01-01') FROM DUAL;3. ADD_MONTHS 函数:对指定日期增加指定的月数。
4. TRUNC 函数:截取日期到指定的精度,例如截取到月份。
5. TO_CHAR 函数:将日期类型转换为字符串类型。
SELECT TO_CHAR(SYSDATE, 'yyyy-mm-dd hh24:mi:ss') FROM DUAL;结果为:2021-08-01 14:00:00四、聚合函数1. COUNT 函数:计算查询结果集中的行数。
ORACLE函数大全
ORACLE 函数大全SQL 中的单记录函数1.ASCII返回与指定的字符对应的十进制数;SQL> select ascii('A') A,ascii('a') a,ascii('0') zero,ascii(' ') space from dual;A A ZERO SPACE--------- --------- --------- ---------659748322.CHR给出整数 , 返回对应的字符;SQL> select chr(54740) zhao,chr(65) chr65 from dual;ZH C-- -赵A3.CONCAT连结两个字符串 ;SQL> select concat('010-','88888888')||'转 23'高乾竞电话 from dual;高乾竞电话----------------转234.INITCAP返回字符串并将字符串的第一个字母变成大写;SQL> select initcap('smith') upp from dual;UPP-----Smith5.INSTR(C1,C2,I,J)在一个字符串中搜寻指定的字符, 返回发现指定的字符的地点;C1被搜寻的字符串C2希望搜寻的字符串I搜寻的开始地点 , 默以为 1J出现的地点 , 默以为 1SQL> select instr('oracle traning','ra',1,2) instring from dual;INSTRING---------9返回字符串的长度;SQL> select name,length(name),addr,length(addr),sal,length(to_char(sal)) from gao.nchar_tst;NAME LENGTH(NAME) ADDR LENGTH(ADDR)SAL LENGTH(TO_CHAR(SAL))------ ------------ ---------------- ------------ --------- --------------------高乾竞3北京市海锭区7返回字符串 , 并将全部的字符小写SQL> select lower('AaBbCcDd')AaBbCcDd from dual;AABBCCDD--------aabbccdd返回字符串 , 并将全部的字符大写SQL> select upper('AaBbCcDd') upper from dual;UPPER--------AABBCCDD和 LPAD(粘贴字符 )RPAD在列的右侧粘贴字符LPAD在列的左侧粘贴字符SQL> select lpad(rpad('gao',10,'*'),17,'*')from dual;LPAD(RPAD('GAO',1-----------------*******gao*******不够字符则用* 来填满10.LTRIM 和 RTRIMLTRIM删除左侧出现的字符串RTRIM删除右侧出现的字符串SQL> select ltrim(rtrim('gao qian jing',' '),' ') from dual;LTRIM(RTRIM('-------------gao qian jing11.SUBSTR(string,start,count)取子字符串 , 从 start开始,取count个SUBSTR('--------0888888812.REPLACE('string','s1','s2')string希望被替代的字符或变量s1被替代的字符串s2要替代的字符串SQL> select replace('he love you','he','i') from dual;REPLACE('H----------i love you返回一个与给定的字符串读音同样的字符串SQL> create table table1(xm varchar(8));SQL> insert into table1 values('weather');SQL> insert into table1 values('wether');SQL> insert into table1 values('gao');SQL> select xm from table1 where soundex(xm)=soundex('weather');XM--------weatherwether14.TRIM('s' from 'string')LEADING剪掉前方的字符TRAILING剪掉后边的字符假如不指定 , 默以为空格符SQL> select TRIM ( 's'from'string') from dual; TRIM ( 's'from'string')tring返回指定值的绝对值SQL> select abs(100),abs(-100) from dual;ABS(100) ABS(-100)--------- ---------100100给出反余弦的值SQL> select acos(-1) from dual;ACOS(-1)---------给出反正弦的值SQL> select asin(0.5) from dual;ASIN(0.5)---------.52359878返回一个数字的反正切值SQL> select atan(1) from dual;ATAN(1)---------.78539816返回大于或等于给出数字的最小整数SQL> select ceil(3.1415927) from dual; CEIL(3.1415927)---------------4返回一个给定数字的余弦SQL> select cos(-3.1415927) from dual;COS(-3.1415927)----------------1返回一个数字反余弦值SQL> select cosh(20) from dual;COSH(20)---------242582598返回一个数字 e 的 n 次方根SQL> select exp(2),exp(1) from dual;EXP(2)EXP(1)--------- ---------对给定的数字取整数SQL> select floor(2345.67) from dual;FLOOR(2345.67)--------------2345返回一个数字的对数值SQL> select ln(1),ln(2),ln(2.7182818) from dual;LN(1)LN(2) LN(2.7182818)--------- --------- -------------0 .69314718.9999999925.LOG(n1,n2)返回一个以n1 为底 n2 的对数SQL> select log(2,1),log(2,4) from dual;LOG(2,1)LOG(2,4)--------- ---------0226.MOD(n1,n2)返回一个n1 除以 n2 的余数SQL> select mod(10,3),mod(3,3),mod(2,3) from dual;MOD(10,3)MOD(3,3)MOD(2,3)--------- --------- ---------102返回 n1 的 n2 次方根SQL> select power(2,10),power(3,3) from dual;POWER(2,10) POWER(3,3)----------- ----------102427和 TRUNC依据指定的精度进行舍入SQL> select round(55.5),round(-55.4),trunc(55.5),trunc(-55.5) from dual; ROUND(55.5) ROUND(-55.4) TRUNC(55.5) TRUNC(-55.5)----------- ------------ ----------- ------------56-5555-55取数字 n 的符号 , 大于 0 返回 1, 小于 0 返回 -1,等于0 返回0 SQL> select sign(123),sign(-100),sign(0) from dual;SIGN(123) SIGN(-100)SIGN(0)--------- ---------- ---------1-10返回一个数字的正弦值SQL> select sin(1.57079) from dual;SIN(1.57079)------------1返回双曲正弦的值SQL> select sin(20),sinh(20) from dual;SIN(20)SINH(20)--------- ---------.91294525 242582598返回数字n 的根SQL> select sqrt(64),sqrt(10) from dual;SQRT(64)SQRT(10)--------- ---------返回数字的正切值SQL> select tan(20),tan(10) from dual;TAN(20)TAN(10)--------- ---------2.2371609 .64836083返回数字n 的双曲正切值SQL> select tanh(20),tan(20) from dual;TANH(20)TAN(20)--------- ---------依据指定的精度截取一个数SQL> select trunc(124.1666,-2) trunc1,trunc(124.16666,2) from dual;TRUNC1 TRUNC(124.16666,2)--------- ------------------100增添或减去月份SQL> select to_char(add_months(to_date('199912','yyyymm'),2),'yyyymm') fromdual;TO_CHA------200002SQL> select to_char(add_months(to_date('199912','yyyymm'),-2),'yyyymm')from dual;TO_CHA------199910返回日期的最后一天SQL> select to_char(sysdate,'yyyy.mm.dd'),to_char((sysdate)+1,'yyyy.mm.dd')from dual;TO_CHAR(SY TO_CHAR((S---------- ----------SQL> select last_day(sysdate) from dual;LAST_DAY(S----------31-5月-0438.MONTHS_BETWEEN(date2,date1)给出 date2-date1的月份SQL> select months_between('19-12月 -1999','19-3月 -1999') mon_between from dual;MON_BETWEEN-----------9SQL>selectmonths_between(to_date('2000.05.20','yyyy.mm.dd'),to_date('2005.05.20','yyyy.mm.dd')) mon_betw from dual;MON_BETW----------6039.NEW_TIME(date,'this','that')给出在 this时区=other时区的日期和时间SQL> select to_char(sysdate,'yyyy.mm.dd hh24:mi:ss')bj_time,to_char(new_time2 (sysdate,'PDT','GMT'),'yyyy.mm.dd hh24:mi:ss') los_angles from dual;BJ_TIME LOS_ANGLES------------------- -------------------2004.05.09 11:05:32 2004.05.09 18:05:3240.NEXT_DAY(date,'day')给出日期date和礼拜x以后计算下一个礼拜的日期SQL> select next_day('18-5月-2001','礼拜五') next_day from dual;NEXT_DAY----------25-5月-01用来获得系统的目前日期SQL> select to_char(sysdate,'dd-mm-yyyy day') from dual;TO_CHAR(SYSDATE,'-----------------09-05-2004礼拜日trunc(date,fmt) 依据给出的要求将日期截断 ,假如 fmt='mi' 表示保存分 ,截断秒 SQL> select to_char(trunc(sysdate,'hh'),'yyyy.mm.dd hh24:mi:ss') hh,2 to_char(trunc(sysdate,'mi'),'yyyy.mm.dd hh24:mi:ss') hhmm from dual;HH HHMM------------------- -------------------2004.05.09 11:00:00 2004.05.09 11:17:00将字符数据种类变换为ROWID种类SQL> select rowid,rowidtochar(rowid),ename from scott.emp;ROWID ROWIDTOCHAR(ROWID) ENAME------------------ ------------------ ----------AAAAfKAACAAAAEqAAA AAAAfKAACAAAAEqAAA SMITH AAAAfKAACAAAAEqAAB AAAAfKAACAAAAEqAAB ALLEN AAAAfKAACAAAAEqAAC AAAAfKAACAAAAEqAAC WARD AAAAfKAACAAAAEqAAD AAAAfKAACAAAAEqAAD JONES43.CONVERT(c,dset,sset)将源字符串sset从一个语言字符集变换到另一个目的dset字符集SQL> select convert('strutz','we8hp','f7dec') "conversion" from dual; conver------strutz将一个十六进制组成的字符串变换为二进制将一个二进制组成的字符串变换为十六进制将 ROWID数据种类变换为字符种类47.TO_CHAR(date,'format')SQL> select to_char(sysdate,'yyyy/mm/dd hh24:mi:ss') from dual;TO_CHAR(SYSDATE,'YY-------------------2004/05/09 21:14:4148.TO_DATE(string,'format')将字符串转变成ORACLE中的一个日期将字符串中的单字节字符转变成多字节字符SQL>select to_multi_byte('高 ') from dual;TO--高将给出的字符变换为数字SQL> select to_number('1999') year from dual;YEAR---------199951.BFILENAME(dir,file)指定一个外面二进制文件SQL>insert into file_tb1 values(bfilename('lob_dir1','image1.gif'));52.CONVERT('x','desc','source')将x 字段或变量的源 source 变换为 desc SQL>select sid,serial#,username,decode(command,2 0,'none',3 2,'insert',4 3,5 'select',6 6,'update',77,'delete',88,'drop',9 'other') cmd from v$session where type!='background';SID SERIAL# USERNAME CMD--------- --------- ------------------------------ ------11none21none31none41none51none61none71275none81275none920 GAO select1040 GAO none53.DUMP(s,fmt,start,length)DUMP 函数以 fmt指定的内部数字格式返回一个VARCHAR2种类的值SQL> col global_name for a30SQL> col dump_string for a50SQL> set lin 200SQL> select global_name,dump(global_name,1017,8,5) dump_stringfrom global_name;GLOBAL_NAME DUMP_STRING------------------------------ --------------------------------------------------Typ=1 Len=12 CharacterSet=ZHS16GBK: W,O,R,L,D54.EMPTY_BLOB()和 EMPTY_CLOB()这两个函数都是用来对大数据种类字段进行初始化操作的函数返回一组表达式中的最大值, 即比较字符的编码大小.SQL> select greatest('AA','AB','AC') from dual;GR--ACSQL> select greatest('啊 ','安 ','天 ') from dual;GR--天返回一组表达式中的最小值SQL> select least('啊','安','天') from dual;LE--啊返回表记目前用户的独一整数SQL> show userUSER为"GAO"SQL> select username,user_id from dba_users where user_id=uid;USERNAME USER_ID------------------------------ ---------GAO25返回目前用户的名字SQL> select user from dual;USER------------------------------GAO返回目前用户环境的信息,opt能够是:ENTRYID,SESSIONID,TERMINAL,ISDBA,LABLE,LANGUAGE,CLIENT_INFO,LANG,VS IZEISDBA查察目前用户是不是DBA 假如是则返回trueSQL> select userenv('isdba') from dual;USEREN------FALSESQL> select userenv('isdba') from dual;USEREN------TRUESESSION返回会话标记SQL> select userenv('sessionid') from dual;USERENV('SESSIONID')--------------------152ENTRYID返回会话人口标记SQL> select userenv('entryid') from dual;USERENV('ENTRYID')------------------INSTANCE返回目前INSTANCE的标记SQL> select userenv('instance') from dual;USERENV('INSTANCE')-------------------1LANGUAGE返回目前环境变量SQL> select userenv('language') from dual;USERENV('LANGUAGE')----------------------------------------------------LANG返回目前环境的语言的缩写SQL> select userenv('lang') from dual;USERENV('LANG')----------------------------------------------------ZHSTERMINAL返回用户的终端或机器的标记SQL> select userenv('terminal') from dual;USERENV('TERMINA----------------GAOVSIZE(X)返回 X 的大小 ( 字节 ) 数SQL> select vsize(user),user from dual;VSIZE(USER) USER----------- ------------------------------6 SYSTEM60.AVG(DISTINCT|ALL)all 表示对全部的值求均匀值 ,distinct 只对不一样的值求均匀值SQLWKS> create table table3(xm varchar(8),sal number(7,2));语句已办理。
oracle常用函数简介
1. 字符串函数•CONCAT: 连接两个字符串。
•SELECT CONCAT('Hello', ' World') FROM dual; -- 结果: 'Hello World'•SUBSTR: 提取子字符串。
•SELECT SUBSTR('Oracle', 2, 3) FROM dual; -- 结果: 'rac'•INSTR: 返回子字符串在字符串中的位置。
•SELECT INSTR('Oracle Database', 'Database') FROM dual; -- 结果: 8•LENGTH: 返回字符串的长度。
•SELECT LENGTH('Oracle') FROM dual; -- 结果: 6•REPLACE: 替换字符串中的子字符串。
•SELECT REPLACE('Oracle Database', 'Database', 'RDBMS') FROM dual; -- 结果: 'Oracle RDBMS'•UPPER: 将字符串转换为大写。
•SELECT UPPER('oracle') FROM dual; -- 结果: 'ORACLE'•LOWER: 将字符串转换为小写。
•SELECT LOWER('ORACLE') FROM dual; -- 结果: 'oracle'•TRIM: 去除字符串两端的空格或指定字符。
•SELECT TRIM(' Oracle ') FROM dual; -- 结果: 'Oracle'2. 数值函数•ROUND: 对数值进行四舍五入。
•SELECT ROUND(123.456, 2) FROM dual; -- 结果: 123.46•TRUNC: 截取数值的小数部分。
ORACLE常用数值函数、转换函数、字符串函数介绍.docx
ORACLE USES numeric functions, transformation functions, and string functions・Numerical function:The absolute value of abs (m) mMod m (n) m is the remainder of nPower (m, n) m to the nRound (m [n]) m four round the decimal point (n default is 0).Trunc (m [n]) m intercepts the value of n bits (n defaults to 0)Character function:Initcap (st) returns st to capitalize the first letter of each word, all other letter lowercaseLower (st) returns st to all lowercase letters of each word Upper (st) returns st to capitalize the letters of each wordConcat (stl, st2) returns st to st2 to the end of stl (the available operator 〃| |〃)The lpad (stl, n [, st2]) returns the right-aligned st, which is filled to the left of stl with st2 filling up until the length is n, and the default for st2 is spaceThe rpad (stl, n [, st2]) returns the left aligned st, which is filled to the right of stl with st2 filling up until the length is n, and the default of st2 is whitespaceLtrim (st [, set]) returns st, which removes the character from the set from the left until the first is not a character in the set. By default, it means spaceRtrim (st [, set]) returns st, which removes the character from the set from the right until the first is not a character in the set・By default, it means spaceReplace (st, search_st [, replace_st]) replaces the search_st with replace_st in st and returns a st. By default, delete search_st substr (st, m [, n]) n = the substring of the st string, starting at m and taking n characters long. By default, you always return to the st endThe length (st) value, which returns the number of characters in stInstr (stl, st2 [, m [, n]]) numeric, returns stl from the m character, st2 the NTH occurrence, the default value of m and nisi:1 select initcap (' THOMAS ') from test; The initca initca -- 一一一一一一Thomas ThomasSelect concat (' ABC 'def') 'first' from test; First -- -abcdef Select 'ABC' | | ' | |' def ‘〃first "from test・ First — ABC defThe select lpad (name, 10), the rpad (name, 5, ' *') from test;The lpad (name, 10) rpad (name, 5, ' *') -------MMX MMX * abcdef abodeRemove the dot and the word st and rd select rtrim (address, '・ st rd ') from testSelect name, replace (name, 'a', ') from test; Name,'『,'a', '--------- g reat gre * tSelect substr (' Archibald bearisol ', 6, 9) a, substr (' Archibald bearisol 11) Bald bear bearisolSelect name, instr (name, ') a, instr (name, ', ', 1, 2) b from test; Name a to b ----------------------------------------------- ---------------------------- l i lei 3 0 I 1 1 2 4%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Conversion function:NVL (m, n) if the m value is null, return n, otherwise the mis returnedTo_char (m [, FMT]) m from a numeric value converted to thespecified format string FMT by default, the width of the FMT value just can accommodate all of the valid number to_number (st [, FMT]) st from character data into a numerical value in the specified format,Default coincided with the size of the numeric format string for the whole number of attached: to_char () function formats:symbolic description -------------------------------------- 9 each 9 represents the result of a number is 0 to display the forerunner 0 $$symbol printed on the left side of the number of L any local currency symbo 1. Print the decimal point, printed on behalf of micrometer a comma-------------------------------------- c ases:Select to_number (' 123・ 45 ') + to_number (' 234. 56 ') form test:To_number (' 123・ 45 ') + to_number (' 234. 56 ') ---------------2. 358・ 01 the select to_char (987654321) from the test. To_char (987654321) 987654321-3. Select to_char (123, '$9999999'). A, to_char (54321, '$9999999') b, to_char (9874321, ' $9999999') c from the test. Abe---------- $123 $54321 $54321 4. Select to_char (1234. 1234, 999999999,). A, to_char (0. 4567, ' 999999999’)b, to_char (1. 1,999999999,) from the test. Abe—1234123. 457 1. 100Group functions:The average of the column nCount ([all] *) returns the number of rows within the query range including the duplicate and null valuesCount ([distinct/all] n) the number of rows of non-null valuesMax ([distinct/all] n) the maximum value of the column or expressionMin ([distinct/all] n) the minimum value of the column or expressionStdev ([distinct/all] n) the standard deviation of the column or expression, ignoring the null valueSum ([distinct/all] n) the sum of the column or expressionVariance (distinct/all n) the variance of the column or expression, ignore null -----------------------------------------------------Date function: >Addjnonths (d, n) date d plus n monthsLast day (d) contains the date of the last day of the month of dMonth_between (d, e) date d and e, e before dNew_time (d, a, b) a time zone and date and time in b time zoneNext_day (d, day) is later than date d, the date specified by daySysdate current system date and timeThe last date in the list of dates given by the greatest (dl,d2, . . . dn)The earliest date in the list of dates given by the least (dl,k2, . . . dn)The to_char (d [FMT]) date is converted to a string in the format specified by FMTThe to_date (st [FMT]) string is converted to a date value in the format specified by FMT, and if the FMT is ignored, the stshould be in the default formatThe round (d [FMT]) date d is specified in FMT to the nearest dateThe trunc (d [FMT]) date is truncated to the latest date by FMT:Date format: ----------------Format codeinstructionsFor example, or the range of valuesDD is 1-3 times a dayThe three capital letters of the week, SUN,・・・ The SAT DAY is a full week. SATURDAY MM January 1-12 MON 3A capital letter of the month JAN,... DEC full JANUARY,..・DECEMBER RM the Roman numeral I,...Assist YY or YYYY two, four digits in HH: MI: SS: poin ts: seco nds HH12 or HH24 in 12 hours or 24 hours according to MI SS seconds indicator on AM or PM afternoon SP suffix SP requirements spell out any numeric fields TH suffix TH said add Numbers is ordinal nu mber 4 TH, 1 st FM prefix for month or day or year values, ban fill 一一一一一一一一一一一一一一一一一一- cases:Next Frida.y,s date, select next_day (6) from test・ Two months ago today's date, select add_months (-2) from test;。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
ORACLE PL/SQL 字符串函数、数学函数、日期函数
--【字符串函数】
--字符串截取substr(字段名,起始点,个数)
select Name,substr(Name,2,4),substr(Name,0,3),substr(Name,-2,3),substr(Name,-2,1) from t1;
--字符串从前面取三个(0开始)
select Name,substr(Name,0,3) from t1;
--字符串从后面取三个
select Name,substr(Name,-3,3),length(Name) 串长度 from t1;
SELECT ASCII('A'),ASCII('B') from dual;
select CHR(100),CHR(80) from dual;
select CONCAT(CHR(65),CONCAT(CHR(67),CHR(98))) from dual;
select CHR(65)||CHR(66)||CHR(76) from dual;
--将每个单词的第一个字母大写其它字母小写返回。
select INITCAP('substr,abc,substring') from dual;
--返回i在MISSISSIPPI中第3次出现的位置,
select INSTR('Mississippi','i',5,3) from dual;
select INSTR('Mississippi','i',-2,3) from dual;
--返回的是字节
select INSTRB('Mississippi','i',5,3) from dual;
select INSTRB('Mississippi','i',-2,3) from dual;
--长度
select length('WHO ARE YOU') from dual;
select nvl(null,'空') from dual;
--小写
select lower('WHo are You') from dual;
--LPAD左侧用字符串补足到一定长度
select LPAD('DFSDf................',9,'WHO') from dual;
select LPAD('DFSD',9,'WHO') from dual;
select LPAD('DFSD',9,'') from dual;
select LPAD('DFSD',length('DFSD')+length('WHO......'),'WHO......') from dual;
--把最左边的字符去掉,使其第一个字符不在其中
select ltrim('Mississippi','Mis') from dual;
select ltrim('Mississippi','miD') from dual;
--RPAD右侧用字符串补足到一定长度
select RPAD('DFSDf................',9,'WHO') from dual;
select RPAD('DFSD',9,'WHO') from dual;
select RPAD('DFSD',9,'') from dual;
select RPAD('DFSD',length('DFSD')+length('WHO......'),'WHO......') from dual;
--把最右边的字符去掉,使其第一个字符不在其中
select Rtrim('Mississippi','Mis') from dual;
select Rtrim('Mississippi','miD') from dual;
--替换
select REPLACE('uptown','up','down') from dual;
--substr和substrb
select SUBSTR('Message',1,4) from dual;
select SUBSTR('ABCDEFG',5) from dual;
select substrb('国际劳动节国营农场',5) from dual;
select substrb('国际劳动节国营农场',2) from dual;
select substrb('国际劳动节国营农场',4,8) from dual;
select substrb('国际劳动节国营农场',3,8) from dual;
--发音
select SOUNDEX('dawes') Dawes,SOUNDEX('daws') Daws, SOUNDEX('dawson') from dual;
--translate
select TRANSLATE('ABCDefghijklmn','eg','替代') test from dual;
select TRIM(' Space padded 0..o0.. ') trim from dual;
select UPPER('abDCCfasdfsdafasdf') from dual;
--【数学函数】所有函数都有数字参数并返回数字值。所有三角函数的操作数和值都是弧度而不是角度,
--oracle没有提供内建的弧度和角度的转换函数。
select abs(-10.234) from dual;
select acos(-1),acos(1) from dual;
select asin(1),asin(0) from dual;
select atan(1),atan(-1) from dual;
select ceil(-2),ceil(5.1) from dual;
select cos(-1) from dual;
select cosh(1) from dual;
select exp(3),exp(2),exp(1) from dual;
select Floor(-3.2),floor(5.91) from dual;
select LN(2) from dual;
select Log(10,2),log(2,10),log(10,1000) from dual;
select MOD(19,3),MOD(20,3),MOD(21,3) from dual;
select POWER(3,4),POWER(4,2) from dual;
select ROUND(12345,-2),ROUND(12345.54321,2),ROUND(-234234.9800234) FROM dual;
select SIGN(-10),SIGN(0),SIGN(1000) from dual;
select sin(1.57) from dual;
select sinh(2.34) from dual;
select SQRT(2),sqrt(3) from dual;
select TAN(1.2) from dual;
select TANH(2.32) from dual;
select trunc(2.2342342,5),trunc(2.3234234) from dual;
--【日期函数】操作DATE数据类型,绝大多数都有DATE数据类型的参数
--增加月
select sysdate,Add_MONTHS(sysdate,1),
Add_MONTHS(TO_Date('2008-05-31 18:18:18','yyyy-MM-dd HH24:MI:SS'),1),
Add_MONTHS(TO_Date('2008-05-31 18:18:18','yyyy-MM-dd HH24:MI:SS'),2)
from dual;
--每月最后一天 只区别了日期, 时间没有区别
select Last_day(sysdate) from dual;
--如果d1和d2的日的日期都相同,或者都使该月的最后一天,那么将返回一个整数,否则会返回的结果将
包含一个分数。
select MONTHS_BETWEEN(sysdate,To_Date('2008-05-31 14:40:00','yyyy-MM-dd HH24:MI:SS')) from
dual;
select NEW_TIME(sysdate,'GMT','PST') from dual;
select NEXT_DAY(To_Date('2007-06-04','yyyy-MM-dd'),'Monday') "1st Monday"
from dual;
select ROUND(sysDate,'yyyy'),ROUND(sysDate,'mm'),ROUND(sysdate,'dd'),
ROUND(sysdate,'HH24'),ROUND(sysDate,'MI')
from dual;
select sysdate from dual;
select trunc(sysdate),trunc(sysdate,'HH24'),trunc(sysdate,'MI') from dual;