Oracle函数详解(经典)

合集下载

oracle instr 用法

oracle instr 用法

oracle instr 用法Oracle INSTR函数的用法详解在Oracle数据库中,INSTR函数是一个非常有用的字符串处理函数。

它用于查找一个字符串在另一个字符串中第一次出现的位置,并返回一个整数值。

本文将深入探讨INSTR函数的用法,并提供详细的步骤和示例来解释每个用法。

1. 基本语法:INSTR函数的基本语法如下:sqlINSTR(string, substring [,start_position [, nth_appearance]])其中:- string 是要在其中搜索的字符串。

- substring 是要搜索的子字符串。

- start_position 是开始搜索的位置,默认为1。

- nth_appearance 是要搜索的第n次出现的位置,默认为1。

2. 返回值:INSTR函数的返回值是指定子字符串第一次出现的位置,位置从1开始计算。

如果找不到子字符串,则返回0。

3. 用法示例:(a) 查找子字符串的位置:例如,我们有一个字符串"Hello World",并且想要找出子字符串"World"的位置。

我们可以使用以下SQL语句:sqlSELECT INSTR('Hello World', 'World') AS position FROM dual;结果将返回5,因为子字符串"World"在原字符串中的第5个位置开始。

(b) 指定开始位置:我们可以使用INSTR函数的第三个参数来指定开始搜索的位置。

例如,我们有一个字符串"Hello World,Hello Universe",我们只想查找"Universe"的位置。

我们可以使用以下SQL语句:sqlSELECT INSTR('Hello World, Hello Universe', 'Universe', 13) AS position FROM dual;结果将返回23,因为我们从位置13开始搜索,找到了子字符串"Universe"在原字符串中的第23个位置。

ORACLE_分析函数大全

ORACLE_分析函数大全

ORACLE_分析函数大全Oracle分析函数是一种高级SQL函数,它可以在查询中实现一系列复杂的分析操作。

这些函数可以帮助我们在数据库中执行各种数据分析和报表生成任务。

本文将介绍Oracle数据库中的一些常用分析函数。

1.ROW_NUMBER函数:该函数为查询结果中的每一行分配一个唯一的数字。

可以用它对结果进行排序或分组。

例如,可以使用ROW_NUMBER函数在结果集中为每个员工计算唯一的编号。

2.RANK和DENSE_RANK函数:这两个函数用于计算结果集中每个行的排名。

RANK函数返回相同值的行具有相同的排名,并且下一个排名值将被跳过。

DENSE_RANK函数类似,但是下一个排名值不会被跳过。

G和LEAD函数:LAG函数返回结果集中指定列的前一个(上一个)行的值,而LEAD函数返回后一个(下一个)行的值。

这些函数通常用于计算增长率或发现趋势。

4.FIRST和LAST函数:这两个函数用于返回结果集中分组的第一个和最后一个行的值。

可以与GROUPBY子句一起使用。

5.CUME_DIST函数:该函数用于计算给定值的累积分布。

它返回值的累积分布在结果集中的位置(百分比)。

6.PERCENT_RANK函数:该函数用于计算结果集中每个行的百分位数排名。

它返回值的百分位数排名(0到1之间的小数)。

7. NTILE函数:该函数用于将结果集分成指定数量的桶(Bucket),并为每个行分配一个桶号。

通常用于将数据分组为更小的块。

8.LISTAGG函数:该函数将指定列的值连接成一个字符串,并使用指定的分隔符分隔每个值。

可以用它将多个值合并在一起形成一个字符串。

9.AVG、SUM、COUNT和MAX/MIN函数:这些是常见的聚合函数,可以在分析函数中使用。

它们用于计算结果集中的平均值、总和、计数和最大/最小值。

以上只是Oracle数据库中的一些常用分析函数。

还有其他一些分析函数,如PERCENTILE_CONT、PERCENTILE_DISC等可以用于更高级的分析计算。

oracle常用函数使用大全Oracle除法(转)

oracle常用函数使用大全Oracle除法(转)

oracle常⽤函数使⽤⼤全Oracle除法(转)/chenmeng2192089/article/details/9155625⼀、运算符算术运算符:+ - * / 可以在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); --包括重复⾏⼆.ORACLE⽇期时间函数⼤全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 ....1. ⽇期和字符转换函数⽤法(to_date,to_char)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//2. select to_char( to_date(222,'J'),'Jsp') from dual显⽰Two Hundred Twenty-Two3.求某天是星期⼏select 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')4. 两个⽇期间的天数select floor(sysdate - to_date('20020405','yyyymmdd')) from dual;5. 时间为null的⽤法select id, active_date from table1UNIONselect 1, TO_DATE(null) from dual;注意要⽤TO_DATE(null)6.⽉份差a_date between to_date('20011201','yyyymmdd') and to_date('20011231','yyyymmdd')那么12⽉31号中午12点之后和12⽉1号的12点之前是不包含在这个范围之内的。

oracle 常用字符串函数

oracle 常用字符串函数

oracle 常用字符串函数Oracle是一种广泛使用的关系型数据库管理系统,它提供了许多强大的函数和工具来处理和管理数据。

在Oracle中,字符串函数是最常用的函数之一,它们可以帮助我们处理和操作字符串数据。

在本文中,我们将介绍一些常用的Oracle字符串函数。

1. SUBSTR函数SUBSTR函数用于从字符串中提取子字符串。

它的语法如下:SUBSTR(string, start_position, [length])其中,string是要提取子字符串的字符串,start_position是子字符串的起始位置,length是要提取的子字符串的长度(可选)。

如果省略length,则将提取从start_position开始的所有字符。

例如,以下语句将从字符串“Hello World”中提取“World”:SELECT SUBSTR('Hello World', 7) FROM dual;输出结果为:“World”。

2. INSTR函数INSTR函数用于查找字符串中某个子字符串的位置。

它的语法如下:INSTR(string, substring, [start_position], [nth_appearance])其中,string是要查找的字符串,substring是要查找的子字符串,start_position是查找的起始位置(可选),nth_appearance是要查找的子字符串的第几个出现位置(可选,默认为1)。

例如,以下语句将查找字符串“Hello World”中第一个出现的字母“o”的位置:SELECT INSTR('Hello World', 'o') FROM dual;输出结果为:5。

3. CONCAT函数CONCAT函数用于连接两个或多个字符串。

它的语法如下:CONCAT(string1, string2, ...)其中,string1、string2等是要连接的字符串。

oracle进一法的函数

oracle进一法的函数

oracle进一法的函数摘要:一、Oracle 进一法函数简介1.函数定义2.常见用途二、Oracle 进一法函数的使用1.语法与参数2.示例与应用三、总结正文:一、Oracle 进一法函数简介在Oracle 数据库中,进一法函数(ROUNDUP)是一种四舍五入函数,用于将数字向上舍入到最接近的整数。

它可以将数值表达式的值四舍五入到最接近的整数,从而满足财务、统计等领域的需求。

1.函数定义Oracle 进一法函数的定义如下:```ROUNDUP(number, significance)```其中,`number` 是需要进行四舍五入的数值表达式,`significance` 是指定的舍入精度。

2.常见用途进一法函数在实际应用中主要用于以下场景:- 向上取整:对于任意实数`x`,`ROUNDUP(x)` 等于大于等于`x` 的最小整数。

- 设置精度:通过指定`significance` 参数,可以控制四舍五入的精度,例如保留两位小数、四舍五入到整百等。

二、Oracle 进一法函数的使用1.语法与参数进一法函数的语法如下:```ROUNDUP(number, significance)```参数说明:- `number`:需要进行四舍五入的数值表达式。

- `significance`:指定舍入精度,可以是一个整数或小数。

如果未指定或为NULL,则默认为0,表示四舍五入到整数。

2.示例与应用以下是一些Oracle 进一法函数的示例:- 向上取整:```SELECT ROUNDUP(5.5) FROM DUAL; -- 结果为6```- 保留两位小数:```SELECT ROUNDUP(5.555, 2) FROM DUAL; -- 结果为5.56```- 四舍五入到整百:```SELECT ROUNDUP(123.456, 100) FROM DUAL; -- 结果为1200```三、总结Oracle 进一法函数(ROUNDUP)是一种实用的四舍五入函数,可以满足不同场景下的需求。

oralce函数

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 条件函数

oracle 条件函数Oracle条件函数是Oracle数据库中一种强大的功能,它能够根据特定的条件来返回相应的结果。

通过使用条件函数,用户可以更灵活地处理和过滤数据,实现更精确的查询和分析。

本文将介绍一些常用的Oracle条件函数,并探讨它们的用法和特点。

一、CASE函数CASE函数是Oracle中最常用的条件函数之一。

它的作用是根据一个或多个条件来返回不同的结果。

CASE函数有两种形式:简单CASE函数和搜索CASE函数。

1. 简单CASE函数简单CASE函数基于一个表达式的值来判断条件,并返回相应的结果。

它的语法如下:CASE expressionWHEN value1 THEN result1WHEN value2 THEN result2...ELSE resultEND其中,expression是一个表达式,value1、value2等是该表达式可能的值,result1、result2等是对应的结果。

例如,我们可以根据学生的成绩段来给予不同的评价:SELECT name,CASEWHEN score >= 90 THEN '优秀'WHEN score >= 80 THEN '良好'WHEN score >= 60 THEN '及格'ELSE '不及格'END AS evaluationFROM students;2. 搜索CASE函数搜索CASE函数不依赖于一个表达式,而是根据一系列条件来匹配并返回相应的结果。

它的语法如下:CASEWHEN condition1 THEN result1WHEN condition2 THEN result2...ELSE resultEND其中,condition1、condition2等是条件表达式,result1、result2等是对应的结果。

例如,我们可以根据学生的出生年份来判断他们所处的年龄段:SELECT name,CASEWHEN birth_year BETWEEN 2000 AND 2010 THEN '10后'WHEN birth_year BETWEEN 1990 AND 1999 THEN '90后'WHEN birth_year BETWEEN 1980 AND 1989 THEN '80后'ELSE '其他'END AS age_groupFROM students;二、DECODE函数DECODE函数是Oracle中另一个常用的条件函数,它可以根据一个或多个条件来返回不同的结果。

oracle常用sql函数

oracle常用sql函数Oracle是一款非常优秀的数据库管理系统,它提供了很多强大的功能与工具,其中最基本而实用的功能就是SQL函数了。

下面我们将重点介绍一些常用的Oracle SQL函数,帮助读者更好地了解和掌握这些常用的函数,从而更加有效地处理和管理数据。

一、数值型函数1. ABS()函数:用于计算数值的绝对值,比如ABS(-10)会返回10。

2. CEIL()函数:用于向上取整,比如CEIL(3.14)会返回4。

3. FLOOR()函数:用于向下取整,比如FLOOR(3.14)会返回3。

4. MOD()函数:用于求模,比如MOD(12,5)会返回2。

5. ROUND()函数:用于四舍五入,比如ROUND(3.49)会返回3,而ROUND(3.51)会返回4。

二、字符型函数1. CONCAT()函数:用于连接两个字符串,比如CONCAT('hello','world')会返回helloworld。

2. SUBSTR()函数:用于截取字符串,比如SUBSTR('hello',2,3)会返回ell。

3. UPPER()函数:用于将字符串转换成大写,比如UPPER('hello')会返回HELLO。

4. LOWER()函数:用于将字符串转换成小写,比如LOWER('HELLO')会返回hello。

5. TRIM()函数:用于去掉字符串首尾的空格,比如TRIM(' hello ')会返回hello。

三、日期型函数1. CURRENT_DATE函数:用于获取当前日期,比如SELECT CURRENT_DATE FROM DUAL会返回当前日期。

2. SYSDATE函数:用于获取系统当前时间,比如SELECTSYSDATE FROM DUAL会返回系统当前时间。

3. TO_CHAR()函数:用于将日期转换成字符型,比如TO_CHAR(SYSDATE,'yyyy-mm-dd')会返回当前日期的年月日格式。

(完整版)ORACLE函数大全

ORACLE函数大全SQL中的单记录函数1.ASCII返回与指定的字符对应的十进制数;SQL〉 select ascii('A')A,ascii(’a') a,ascii('0’) zero,ascii(' ') space from dual;A A ZERO SPACE————-——-— -—---———- ---—----- ---————-—65 97 48 322.CHR给出整数,返回对应的字符;SQL〉 select chr(54740) zhao,chr(65) chr65 from dual;ZH C—— -赵 A3.CONCAT连接两个字符串;SQL> select concat('010—’,'88888888')||'转23’高乾竞电话 from dual;高乾竞电话—-——-———-—--——-—010—88888888转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—-—------96.LENGTH返回字符串的长度;SQL> select name,length(name),addr,length(addr),sal,length(to_char(sal)) from gao.nchar_tst;NAME LENGTH(NAME) ADDR LENGTH(ADDR) SALLENGTH(TO_CHAR(SAL))————-———---————-—- —--——---——----—- -———--—-—-—— ----———-————----—-——--—--—---高乾竞 3 北京市海锭区 6 9999.99 77。

oracle统计个数的函数

oracle统计个数的函数Oracle是一种强大的数据库管理系统,它提供了许多函数来处理和分析数据。

其中之一就是用于统计个数的函数。

在本文中,我将为您介绍一些常用的Oracle统计个数的函数,并提供相应的示例。

1. COUNT函数COUNT函数用于统计满足指定条件的行数。

它可以作用于单个列、多个列或整个表。

以下是COUNT函数的语法:```SELECT COUNT(column_name) FROM table_name WHERE condition;```其中,column_name是要进行统计的列名,table_name是要进行统计的表名,condition是筛选条件(可选)。

示例1:我们有一个名为"students"的表,其中包含了学生的信息。

现在,我们想统计表中所有学生的个数。

可以使用以下SQL语句:```SELECT COUNT(*) FROM students;```这将返回表中所有学生的个数。

示例2:假设我们只想统计表中英语成绩超过60分的学生个数,可以使用以下SQL语句:```SELECT COUNT(*) FROM students WHERE English > 60;```这将返回英语成绩超过60分的学生个数。

2. SUM函数SUM函数用于计算指定列的数值总和。

它常用于对数值进行求和操作。

以下是SUM函数的语法:```SELECT SUM(column_name) FROM table_name WHERE condition;```其中,column_name是要进行求和的列名,table_name是要进行求和的表名,condition是筛选条件(可选)。

示例3:假设我们有一个名为"sales"的表,其中包含了销售数据。

现在,我们想计算表中所有销售额的总和。

可以使用以下SQL语句:```SELECT SUM(sales_amount) FROM sales;```这将返回表中所有销售额的总和。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Oracle常用函数/过程说明主要介绍Oracle的系统函数、过程和包。

SQL常用函数:数值函数:ABSPurpose 返回绝对值Returns the absolute value of n.ExampleSELECT ABS(-15) "Absolute" FROM DUAL;Absolute----------15CEILPurpose 取最小整数Returns smallest integer greater than or equal to n. ExampleSELECT CEIL(15.7) "Ceiling" FROM DUAL;Ceiling----------16* MODSyntaxMOD(m,n)Purpose 取余Returns remainder of m divided by n. Returns m if n is 0. ExampleSELECT MOD(11,4) "Modulus" FROM DUAL;Modulus----------3* ROUNDSyntaxROUND(n[,m])Purpose 取四舍五入信息Returns n rounded to m places right of the decimal point; if m is omitted, to 0 places. m can be negative to round off digits left of the decimal point. m must be an integer.Example 1SELECT ROUND(15.193,1) "Round" FROM DUAL;Round----------15.2Example 2SELECT ROUND(15.193,-1) "Round" FROM DUAL;Round----------20* TRUNCPurpose 取截取后的信息Returns n truncated to m decimal places; if m is omitted, to 0 places. m can be negative to truncate (make zero) m digits left of the decimal point.ExamplesSELECT TRUNC(15.79,1) "Truncate" FROM DUAL;Truncate----------15.7SELECT TRUNC(15.79,-1) "Truncate" FROM DUAL;Truncate----------10字符函数:* CONCATSyntaxCONCAT(char1, char2)Purpose 合并字符串,相当于“||”Returns char1 concatenated with char2. This function is equivalent to the concatenation operator (||). For information on this operator, see "Concatenation Operator".ExampleThis example uses nesting to concatenate three character strings: SELECT CONCAT( CONCAT(ename, ' is a '), job) "Job"FROM empWHERE empno = 7900;Job-----------------JAMES is a CLERK* LOWERPurpose 变为小写Returns char, with all letters lowercase. The return value has the same datatype as the argument char (CHAR or V ARCHAR2). ExampleSELECT LOWER('MR. SCOTT MCMILLAN') "Lowercase"FROM DUAL;Lowercase--------------------mr. scott mcmillan* LPADPurpose 左填充Returns char1, left-padded to length n with the sequence of characters in char2; char2 defaults to a single blank. If char1 is longer than n, this function returns the portion of char1 that fits in n.The argument n is the total length of the return value as it is displayed on your terminal screen. In most character sets, this is also the number of characters in the return value. However, in some multibyte character sets, the display length of a character string can differ from the number of characters in the string.ExampleSELECT LPAD('Page 1',15,'*.') "LPAD example"FROM DUAL;LPAD example---------------*.*.*.*.*Page 1* LTRIMSyntaxLTRIM(char [,set])Purpose 左截取Removes characters from the left of char, with all the leftmost characters that appear in set removed; set defaults to a single blank. Oracle begins scanning char from its first character and removes all characters that appear in set until reaching a character not in set and then returns the result.ExampleSELECT LTRIM('xyxXxyLAST WORD','xyXLA') "LTRIM example"FROM DUAL;LTRIM exampl------------* REPLACESyntaxREPLACE(char,search_string[,replacement_string])Purpose 替换Returns char with every occurrence of search_string replaced with replacement_string. If replacement_string is omitted or null, all occurrences of search_string are removed. If search_string is null, char is returned. This function provides a superset of the functionality provided by the TRANSLATE function. TRANSLATE provides single-character, one-to-one substitution. REPLACE allows you to substitute one string for another as well as to remove character strings.ExampleSELECT REPLACE('JACK and JUE','J','BL') "Changes"FROM DUAL;Changes--------------BLACK and BLUESyntaxRPAD(char1, n [,char2])Purpose 右填充Returns char1, right-padded to length n with char2, replicated as many times as necessary; char2 defaults to a single blank. If char1 is longer than n, this function returns the portion of char1 that fits in n. The argument n is the total length of the return value as it is displayed on your terminal screen. In most character sets, this is also the number of characters in the return value. However, in some multibyte character sets, the display length of a character string can differ from the number of characters in the string.ExampleSELECT RPAD('MORRISON',12,'ab') "RPAD example"FROM DUAL;RPAD example-----------------MORRISONabab* RTRIMSyntaxRTRIM(char [,set]Returns char, with all the rightmost characters that appear in set removed; set defaults to a single blank. RTRIM works similarly to LTRIM.ExampleSELECT RTRIM('BROWNINGyxXxy','xy') "RTRIM e.g."FROM DUAL;RTRIM e.g-------------BROWNINGyxX* SUBSTRSyntaxSUBSTR(char, m [,n])Purpose 截取字符串Returns a portion of char, beginning at character m, n characters long. If m is 0, it is treated as 1. If m is positive, Oracle counts from the beginning of char to find the first character. If m is negative, Oracle counts backwards from the end of char. If n is omitted, Oracle returns all characters to the end of char. If n is less than 1, a null is returned.Floating-point numbers passed as arguments to substr are automatically converted to integers.Example 1SELECT SUBSTR('ABCDEFG',3,4) "Subs"FROM DUAL;Subs----CDEFExample 2SELECT SUBSTR('ABCDEFG',-5,4) "Subs"FROM DUAL;Subs----CDEF* TRANSLATESyntaxTRANSLATE(char, from, to)Purpose 在一定范围内转换字符Returns char with all occurrences of each character in from replacedby its corresponding character in to. Characters in char that are not in from are not replaced. The argument from can contain more characters than to. In this case, the extra characters at the end of from have no corresponding characters in to. If these extra characters appear in char, they are removed from the return value. You cannot use an empty string for to to remove all characters in from from the return value. Oracle interprets the empty string as null, and if this function has a null argument, it returns null.Example 1The following statement translates a license number. All letters 'ABC...Z' are translated to 'X' and all digits '012 . . . 9' are translated to '9':SELECT TRANSLATE('2KRW229','0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ','9999999999XXXXXXXXXXXXXXXXXXXXXXXXXX') "License"FROM DUAL;License--------9XXX999Example 2The following statement returns a license number with the characters removed and the digits remaining:SELECT TRANSLATE('2KRW229','0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ','0123456789')"Translate example"FROM DUAL;Translate example-----------------2229* UPPERSyntaxUPPER(char)Purpose 大写Returns char, with all letters uppercase. The return value has the same datatype as the argument char.ExampleSELECT UPPER('Large') "Uppercase"FROM DUAL;Upper-----LARGE* ASCIISyntaxASCII(char)Purpose 取字符的ASCII值Returns the decimal representation in the database character set of the first character of char. If your database character set is 7-bit ASCII, this function returns an ASCII value. If your database character set is EBCDIC Code Page 500, this function returns an EBCDIC value. Note that there is no similar EBCDIC character function.ExampleSELECT ASCII('Q')FROM DUAL;ASCII('Q')----------81* INSTRSyntaxINSTR (char1,char2 [,n[,m]])Purpose 从char1中第n个字符开始char2第m次出现的位置Searches char1 beginning with its nth character for the mth occurrence of char2 and returns the position of the character in char1 that is the first character of this occurrence. If n is negative, Oracle counts and searches backward from the end of char1. The value of m must be positive. The default values of both n and m are 1, meaning Oracle begins searching at the first character of char1 for the first occurrence of char2. The return value is relative to the beginning of char1, regardless of the value of n, and is expressed in characters. If the search is unsuccessful (if char2 does not appear m times after the nth character of char1) the return value is 0.Example 1SELECT INSTR('CORPORATE FLOOR','OR', 3, 2) "Instring" FROM DUAL;Instring----------14Example 2SELECT INSTR('CORPORATE FLOOR','OR', -3, 2)"Reversed Instring"FROM DUAL;Reversed Instring-----------------2* LENGTHSyntaxLENGTH(char)Purpose 取字符串的长度Returns the length of char in characters. If char has datatype CHAR, the length includes all trailing blanks. If char is null, this function returns null.ExampleSELECT LENGTH('CANDIDE') "Length in characters"FROM DUAL;Length in characters--------------------7* ADD_MONTHSSyntaxADD_MONTHS(d,n)Purpose 取N个月后的日期Returns the date d plus n months. The argument n can be any integer. If d is the last day of the month or if the resulting month has fewer days than the day component of d, then the result is the last day of the resulting month. Otherwise, the result has the same day component as d.ExampleSELECT TO_CHAR(ADD_MONTHS(hiredate,1),'DD-MON-YYYY') "Next month"FROM empWHERE ename = 'SMITH';Next Month-----------17-JAN-1981* LAST_DAYSyntaxLAST_DAY(d)Purpose 取D所在月份的最后一天Returns the date of the last day of the month that contains d. You might use this function to determine how many days are left in the current month.Example 1SELECT SYSDATE,LAST_DAY(SYSDATE) "Last",LAST_DAY(SYSDATE) - SYSDATE "Days Left"FROM DUAL;SYSDATE Last Days Left--------- --------- ----------23-OCT-97 31-OCT-97 8Example 2SELECT TO_CHAR(ADD_MONTHS(LAST_DAY(hiredate),5),'DD-MON-YYYY') "Five months"FROM empWHERE ename = 'MARTIN';Five months-----------28-FEB-1982* MONTHS_BETWEENSyntaxMONTHS_BETWEEN(d1, d2)Purpose 取两个日期间相隔的月数Returns number of months between dates d1 and d2. If d1 is later than d2, result is positive; if earlier, negative. If d1 and d2 are either the same days of the month or both last days of months, the result is always an integer; otherwise Oracle calculates the fractional portion of the result based on a 31-day month and considers the difference in time components of d1 and d2.ExampleSELECT MONTHS_BETWEEN(TO_DATE('02-02-1995','MM-DD-YYYY'),TO_DATE('01-01-1995','MM-DD-YYYY') ) "Months"FROM DUAL;Months----------1.03225806* NEXT_DAYSyntax* NEXT_DAY(d, char)Purpose 取D下一个weekday的日期Returns the date of the first weekday named by char that is later than the date d. The argument char must be a day of the week in your session's date language-either the full name or the abbreviation. The minimum number of letters required is the number of letters in the abbreviated version; any characters immediately following the valid abbreviation are ignored. The return value has the same hours, minutes, and seconds component as the argument d.ExampleThis example returns the date of the next Tuesday after March 15,1992.SELECT NEXT_DAY('15-MAR-92','TUESDAY') "NEXT DAY"FROM DUAL;NEXT DAY---------17-MAR-92* SYSDATESyntaxSYSDATEPurpose 取系统日期Returns the current date and time. Requires no arguments. In distributed SQL statements, this function returns the date and time on your local database. You cannot use this function in the condition of a CHECK constraint.ExampleSELECT TO_CHAR(SYSDATE, 'MM-DD-YYYY HH24:MI:SS')"NOW"FROM DUAL;NOW-------------------10-29-1993 20:27:11转换函数:* CHARTOROWIDSyntaxCHARTOROWID(char)Purpose 将字符串转换为ROWIDConverts a value from CHAR or V ARCHAR2 datatype to ROWID datatype.ExampleSELECT ename FROM empWHERE ROWID = CHARTOROWID('AAAAfZAABAAACp8AAO');ENAME----------LEWIS* TO_CHAR, date conversionSyntaxTO_CHAR(d [, fmt [, 'nlsparams'] ])Purpose 将日期转换为字符串Converts d of DATE datatype to a value of V ARCHAR2 datatype in the format specified by the date format fmt. If you omit fmt, d is converted to a V ARCHAR2 value in the default date format. For information on date formats, see "Format Models".ExampleSELECT TO_CHAR(HIREDATE, 'Month DD, YYYY') "New date format" FROM empWHERE ename = 'BLAKE';New date format------------------May 01, 1981*TO_CHAR,number conversionSyntaxTO_CHAR(n [, fmt [, 'nlsparams'] ])Purpose 将数值按一定格式转换为字符串Converts n of NUMBER datatype to a value of V ARCHAR2 datatype, using the optional number format fmt. If you omit fmt, n is converted to a V ARCHAR2 value exactly long enough to hold its significant digits. For information on number formats, see "Format Models". Example 1In this example, the output is blank padded to the left of the currency symbol.SELECT TO_CHAR(-10000,'L99G999D99MI') "Amount"FROM DUAL;Amount--------------$10,000.00-* TO_DATESyntaxTO_DATE(char [, fmt [, 'nlsparams'] ])Purpose 将日期转换为字符串Converts char of CHAR or V ARCHAR2 datatype to a value of DATE datatype. The fmt is a date format specifying the format of char. If you omit fmt, char must be in the default date format. If fmt is 'J', for Julian, then char must be an integer. For information on date formats, see "Format Models".The 'nlsparams' has the same purpose in this function as in the TO_CHAR function for date conversion.Do not use the TO_DATE function with a DATE value for the char argument. The returned DATE value can have a different century value than the original char, depending on fmt or the default date format.For information on date formats, see "Date Format Models". ExampleINSERT INTO bonus (bonus_date)SELECT TO_DATE('January 15, 1989, 11:00 A.M.','Month dd, YYYY, HH:MI A.M.','NLS_DATE_LANGUAGE = American')FROM DUAL;* TO_NUMBERSyntaxTO_NUMBER(char [,fmt [, 'nlsparams'] ])PurposeConverts char, a value of CHAR or V ARCHAR2 datatype containing a number in the format specified by the optional format model fmt, to a value of NUMBER datatype.* NVLSyntaxNVL(expr1, expr2)Purpose 转换null值If expr1 is null, returns expr2; if expr1 is not null, returns expr1. The arguments expr1 and expr2 can have any datatype. If their datatypes are different, Oracle converts expr2 to the datatype of expr1 beforecomparing them. The datatype of the return value is always the same as the datatype of expr1, unless expr1 is character data, in which case the return value's datatype is V ARCHAR2.ExampleSELECT ename, NVL(TO_CHAR(COMM), 'NOT APPLICABLE')"COMMISSION" FROM empWHERE deptno = 30;ENAME COMMISSION---------- -------------------------------------ALLEN 300WARD 500MARTIN 1400BLAKE NOT APPLICABLETURNER 0JAMES NOT APPLICABLE* UIDSyntaxUIDPurpose 系统用户的标码Returns an integer that uniquely identifies the current user.* USERSyntaxUSERPurpose 系统用户户名Returns the current Oracle user with the datatype V ARCHAR2. Oracle compares values of this function with blank-padded comparison semantics.In a distributed SQL statement, the UID and USER functions identify the user on your local database. You cannot use these functions in the condition of a CHECK constraint.ExampleSELECT USER, UID FROM DUAL;USER UID------------------------------ ----------SCOTT 19 组函数:* A VGSyntaxA VG([DISTINCT|ALL] n)Purpose 取平均值Returns average value of n.ExampleSELECT A VG(sal) "Average"FROM emp;Average----------2077.21429* COUNTSyntaxCOUNT({* | [DISTINCT|ALL] expr})Purpose 取记录数Returns the number of rows in the query.If you specify expr, this function returns rows where expr is not null. You can count either all rows, or only distinct values of expr.If you specify the asterisk (*), this function returns all rows, including duplicates and nulls.Example 1SELECT COUNT(*) "Total"FROM emp;Total----------18Example 2SELECT COUNT(job) "Count"FROM emp;Count----------14Example 3SELECT COUNT(DISTINCT job) "Jobs"FROM emp;Jobs----------5* MAXSyntaxMAX([DISTINCT|ALL] expr)Purpose 取最大值Returns maximum value of expr.ExampleSELECT MAX(sal) "Maximum" FROM emp;Maximum----------5000* MINSyntaxMIN([DISTINCT|ALL] expr)PurposeReturns minimum value of expr.ExampleSELECT MIN(hiredate) "Earliest" FROM emp;Earliest---------17-DEC-80* SUMSyntaxSUM([DISTINCT|ALL] n)Purpose 求和Returns sum of values of n.ExampleSELECT SUM(sal) "Total"FROM emp;Total----------29081* DBMS_ALERT实现数据库间的警报。

相关文档
最新文档