加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Oracle函数

发布时间:2020-12-12 16:39:31 所属栏目:百科 来源:网络整理
导读:数值型函数 返回绝对值 abs(x) ABS(X)【功能】返回x的绝对值【参数】x,数字型表达式【返回】数字 code class="hljs vhdl has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-fam

数值型函数

返回绝对值 abs(x)

ABS(X)
【功能】返回x的绝对值
【参数】x,数字型表达式
【返回】数字
<code class="hljs vhdl has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro",monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">SQL> <span class="hljs-keyword" style="color: rgb(0,136); box-sizing: border-box;">select</span> <span class="hljs-keyword" style="color: rgb(0,136); box-sizing: border-box;">abs</span>(<span class="hljs-number" style="color: rgb(0,102,102); box-sizing: border-box;">100</span>),<span class="hljs-keyword" style="color: rgb(0,136); box-sizing: border-box;">abs</span>(-<span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">100</span>) from dual ;

  <span class="hljs-keyword" style="color: rgb(0,136); box-sizing: border-box;">ABS</span>(<span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">100</span>)  <span class="hljs-keyword" style="color: rgb(0,136); box-sizing: border-box;">ABS</span>(-<span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">100</span>)
<span class="hljs-comment" style="color: rgb(136,0); box-sizing: border-box;">---------- ----------</span>
       <span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">100</span>        <span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">100</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238,238,238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221,221,221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238,221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

返回正负值 sign(x)

sign(x)
【功能】返回x的正负值
【参数】x,数字型表达式
【返回】数字,若为正值返回1,负值返回-1,0返回0
<code class="hljs scss has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro",monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">SQL> <span class="hljs-tag" style="color: rgb(0,102); box-sizing: border-box;">select</span> <span class="hljs-function" style="box-sizing: border-box;">sign(<span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">100</span>)</span>,<span class="hljs-function" style="box-sizing: border-box;">sign(-<span class="hljs-number" style="color: rgb(0,<span class="hljs-function" style="box-sizing: border-box;">sign(<span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">0</span>)</span> from dual ;

 <span class="hljs-function" style="box-sizing: border-box;">SIGN(<span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">100</span>)</span> <span class="hljs-function" style="box-sizing: border-box;">SIGN(-<span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">100</span>)</span>    <span class="hljs-function" style="box-sizing: border-box;">SIGN(<span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">0</span>)</span>
---------- ---------- ----------
         1         -1          0</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238,221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

返回较大的最小整数 ceil(x)

ceil(x)
【功能】返回大于等于x的最小整数值
【参数】x,数字型表达式
【返回】数字

 

返回较小的最大整数 floor(x)

floor(x)
【功能】返回小于等于x的最大整数值
【参数】x,数字型表达式
【返回】数字

 

返回x的y次幂 power(x,y)

power(x,y)
【功能】返回x的y次幂
【参数】x,y 数字型表达式
【返回】数字

 

【相近】exp(y)
返回e的y次幂。(e为数学常量)

【关系】z=power(x,y),则y=1/log(z,x) (条件z,x>0)


返回常量e的y次幂 exp(y)

exp(y)
【功能】返回e的y次幂(e为数学常量)
【参数】y,数字型表达式
【返回】数字

<code class="hljs vbscript has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro",136); box-sizing: border-box;">select</span> <span class="hljs-built_in" style="color: rgb(102,102); box-sizing: border-box;">exp</span>(<span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">3</span>),<span class="hljs-built_in" style="color: rgb(102,102); box-sizing: border-box;">0</span>),102); box-sizing: border-box;">exp</span>(-<span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">3</span>) from dual ;

    <span class="hljs-built_in" style="color: rgb(102,102); box-sizing: border-box;">EXP</span>(<span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">3</span>)     <span class="hljs-built_in" style="color: rgb(102,102); box-sizing: border-box;">0</span>)    <span class="hljs-built_in" style="color: rgb(102,102); box-sizing: border-box;">EXP</span>(-<span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">3</span>)
---------- ---------- ----------
<span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">20.0855369</span>          <span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">1</span> <span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">0.04978706</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238,221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

【相近】power(x,y)
返回e的y次幂。

【相反】ln(y)
返回e为底的自然对数。


返回以x为底的y的对数 log(x,y)

【功能】返回以x为底的y的对数
【参数】x,y,数字型表达式,
【条件】x,y都必须大于0
【返回】数字

 
 

【相近】ln(y)
返回e为底的y的对数。(e为数学常量)

【关系】z=power(x,x>0)


返回以e为底的y的对数(e为数学常量)

ln(y)
【功能】返回以e为底的y的对数(e为数学常量)
【参数】y,数字型表达式 (条件y>0)
【返回】数字

 

【相近】log(x,y)
返回以x为底的y的对数

【相反】exp(y)
返回e的y次幂


返回x除以y的余数 mod(x,y)

【功能】返回x除以y的余数
【参数】x,y,数字型表达式
【返回】数字

<code class="hljs vbnet has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro",136); box-sizing: border-box;">mod</span>(<span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">23</span>,102); box-sizing: border-box;">8</span>),102); box-sizing: border-box;">24</span>,102); box-sizing: border-box;">8</span>) <span class="hljs-keyword" style="color: rgb(0,136); box-sizing: border-box;">from</span> dual;

 <span class="hljs-keyword" style="color: rgb(0,136); box-sizing: border-box;">MOD</span>(<span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">8</span>)  <span class="hljs-keyword" style="color: rgb(0,102); box-sizing: border-box;">8</span>)
---------- ----------
         <span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">7</span>          <span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">0</span>
</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238,221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>

返回四舍五入后的值 round(x[,y])

round(x[,y])
【功能】返回四舍五入后的值
【参数】x,y,数字型表达式,
如果y不为整数则截取y整数部分,
如果y>0则四舍五入为y位小数,
如果y小于0则四舍五入到小数点向左第y位。
【返回】数字

 

【相近】trunc(x[,y])
返回截取后的值,用法同round(x[,y]),只是不四舍五入


返回x按精度y截取后的值 trun(x[,y])

【功能】返回x按精度y截取后的值
【参数】x,

如果y不为整数则截取y整数部分,
如果y>0则截取到y位小数,
如果y小于0则截取到小数点向左第y位,小数前其它数据用0表示。
【返回】数字

 

【相近】round(x[,y])
返回截取后的值,用法同trunc(x[,只是要做四舍五入


返回x的平方根 sqrt(x)

sqrt(x)
【功能】返回x的平方根
【参数】x数字型表达式
【返回】数字

 

三角函数

SIN(x)

【功能】返回一个数字的正弦值

<code class="hljs asciidoc has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro",monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">SQL> select sin(1.57079) from dual;

<span class="hljs-header" style="box-sizing: border-box;">SIN(1.57079)
------------</span>
0.9999999999</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238,221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

SIGH(x)

【功能】返回双曲正弦的值

 

COS(x)

【功能】返回一个给定数字的余弦

 

COSH(x)

【功能】返回一个数字反余弦值

 

TAN

【功能返回数字的正切值

 

TANH

【功能返回数字n的双曲正切值

 

ASIN(x)

【功能】给出反正弦的值
【示例】select asin(0.5) from dual;
返回:0.52359878

ACOS(x)

【功能】给出反余弦的值

 

ATAN(x)

【功能】返回一个数字的反正切值

 

字符型函数

返回字符表达式最左端字符的ASCII 码值 ASCII(x1)

ASCII(x1)
【功能】:返回字符表达式最左端字符的ASCII 码值。
【参数】:x1,字符表达式
【返回】:数值型

 

【说明】在ASCII()函数中,纯数字的字符串可不用‘’括起来,但含其它字符的字符串必须用‘’括起来使用,否则会出错。
  如果最左端是汉字,只取汉字最左半边字符的ASCII 码

【互反函数】:chr()

返回ASCII为x的字符 chr()

CHR(n1)
【功能】:将ASCII 码转换为字符。
【参数】:n1,为0 ~ 255,整数
【返回】:字符型

<code class="hljs perl has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro",136); box-sizing: border-box;">chr</span>(<span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">54740</span>) zhao,102); box-sizing: border-box;">65</span>) chr65 from dual;

ZHAO CHR65
---- -----
赵   A</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238,221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>

【互反函数】:ASCII


连接两个字符串 concat(c1,c2)

CONCAT(c1,c2)
【功能】连接两个字符串
【参数】c1,c2 字符型表达式
【返回】字符型
同:c1||c2

 

把每个单词的首字个字母变成大写 initcap(c1)

INITCAP(c1)
【功能】返回字符串并将字符串的第一个字母变为大写,其它字母小写;
【参数】c1字符型表达式
【返回】字符型

 

把整个字符串转换为小写 lower(c1)

LOWER(c1)
【功能】:将字符串全部转为小写
【参数】:c1,字符表达式
【返回】:字符型

 

把整个字符串转换为大写 upper(c1)

UPPER(c1)
【功能】将字符串全部转为大写
【参数】c1,字符表达式
【返回】字符型

 

把每个单词首个字母变为大写 nls_initcap(x[,y])

NLS_INITCAP(x[,y])
【功能】返回字符串并将字符串的第一个字母变为大写,其它字母小写;
【参数】x字符型表达式
【参数】Nls_param可选,
查询数据级的NLS设置:select * from nls_database_parameters;

例如:
指定排序的方式(nls_sort=) 。
nls_sort=SCHINESE_RADICAL_M(部首、笔画)
nls_sort=SCHINESE_STROKE_M(笔画、部首SCHINESE_PINYIN_M(拼音))

【返回】字符型

<code class="hljs mathematica has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro",monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">SQL> select nls_initcap(<span class="hljs-string" style="color: rgb(0,0); box-sizing: border-box;">'ab cde'</span>) <span class="hljs-string" style="color: rgb(0,0); box-sizing: border-box;">"test"</span>,nls_initcap(<span class="hljs-string" style="color: rgb(0,0); box-sizing: border-box;">'a c b d e'</span>,<span class="hljs-string" style="color: rgb(0,0); box-sizing: border-box;">'nls_sort= SCHINESE_PINYIN_M'</span>) <span class="hljs-string" style="color: rgb(0,0); box-sizing: border-box;">"test1"</span> from dual;

test   test1
------ ---------
Ab Cde A <span class="hljs-keyword" style="color: rgb(0,136); box-sizing: border-box;">C</span> B <span class="hljs-keyword" style="color: rgb(0,136); box-sizing: border-box;">D</span> <span class="hljs-keyword" style="color: rgb(0,136); box-sizing: border-box;">E</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238,221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>
 

把整个字符串转换为小写 nls_lower(x[,y])

NLS_LOWER(x[,y])
【功能】返回字符串并将字符串的变为小写;
【参数】x字符型表达式
【参数】Nls_param可选,指定排序的方式(nls_sort=) 。
SCHINESE_RADICAL_M(部首、笔画)
SCHINESE_STROKE_M(笔画、部首SCHINESE_PINYIN_M(拼音))
【返回】字符型

<code class="hljs cs has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro",monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">SQL>  <span class="hljs-keyword" style="color: rgb(0,136); box-sizing: border-box;">select</span> nls_LOWER(<span class="hljs-string" style="color: rgb(0,nls_LOWER(<span class="hljs-string" style="color: rgb(0,0); box-sizing: border-box;">"test1"</span> <span class="hljs-keyword" style="color: rgb(0,136); box-sizing: border-box;">from</span> dual;

test   test1
------ ---------
ab cde a c b d e</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238,221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

把整个字符串转换为大写 nls_upper(x[,y])

NLS_UPPER(x[,y])
【功能】返回字符串并将字符串的转换为大写;
【参数】x字符型表达式
【参数】Nls_param可选,指定排序的方式(nls_sort=) 。
SCHINESE_RADICAL_M(部首、笔画)
SCHINESE_STROKE_M(笔画、部首SCHINESE_PINYIN_M(拼音))
【返回】字符型

 

字符串中搜索字符位置(全角算1字符) instr(C1,C2[,I[,J]])

INSTR(C1,J]])
【功能】在一个字符串中搜索指定的字符,返回发现指定的字符的位置;
【说明】多字节符(汉字、全角符等),按1个字符计算
【参数】
C1 被搜索的字符串
C2 希望搜索的字符串
I 搜索的开始位置,默认为1
J 第J次出现的位置,默认为1
【返回】数值

 
 

字符串中搜索字符位置(全角算2字符) instrb(C1,J]])

INSTRB(C1,返回发现指定的字符的位置;
【说明】多字节符(汉字、全角符等),按2个字符计算
【参数】
C1 被搜索的字符串
C2 希望搜索的字符串
I 搜索的开始位置,默认为1
【返回】数值

 

返回字符串的长度(全角算1字符) length(c1)

LENGTH(c1)
【功能】返回字符串的长度;
【说明】多字节符(汉字、全角符等),按1个字符计算
【参数】C1 字符串
【返回】数值型

 

返回字符串的长度(全角算2字符)lengthb(c1)

LENGTH(c1)
【功能】返回字符串的长度;
【说明】多字节符(汉字、全角符等),按2个字符计算
【参数】C1 字符串
【返回】数值型

 

返回字符串的长度(其他)lengthc(c1) length2(c1) length4(c1)

LENGTHC(c1).LENGTH2(c1).LENGTH4(c1)
【功能】返回字符串的长度;
【说明】多字节符(汉字、全角符等),按1个字符计算
【参数】C1 字符串
【返回】数值型

 

Oracle中的字符函数中,有一类函数是求字符长度的函数,length、lengthB、lengthC、length2、length4几个函数中比较常用的是length、lengthB。

他们的含义分别是:
Length函数返回字符的个数,使用定义是给定的字符集来计算字符的个数
LENGTHB给出该字符串的byte
LENGTHC使用纯Unicode
LENGTH2使用UCS2
LENGTH4使用UCS4

 

在左边添加字符 lpad(c1,n[,c2])

LPAD(c1,c2])
【功能】在字符串c1的左边用字符串c2填充,直到长度为n时为止
【参数】C1 字符串
n 追加后字符总长度
c2 追加字符串,默认为空格
【返回】字符型
【说明】如果c1长度大于n,则返回c1左边n个字符
如果如果c1长度小于n,c2和c1连接后大于n,则返回连接后的右边n个字符

 

不够字符则用*来填满

【相似】RPAD()在列的右边粘贴字符
【相反】LTRIM() 删除左边出现的字符串


在右边添加字符 rpad(c1,c2])

RPAD(c1,c2])
【功能】在字符串c1的右边用字符串c2填充,直到长度为n时为止
【参数】C1 字符串
n 追加后字符总长度
c2 追加字符串,默认为空格
【返回】字符型
【说明】如果c1长度大于n,则返回c1左边n个字符
如果如果c1长度小于n,c1和c2连接后大于n,则返回连接后的左边n个字符
如果如果c1长度小于n,c1和c2连接后小于n,则返回c1与多个重复c2连接(总长度>=n)后的左边n个字符

 

【相似】LPAD()在列的左边粘贴字符
【相反】RTRIM() 删除右边出现的字符串


删除左边字符 ltrim(c1,[,c2])

LTRIM(c1,c2])
【功能】删除左边出现的字符串
【参数】C1 字符串
c2 追加字符串,默认为空格
【返回】字符型

 

删除右边字符 rtrim(c1,c2])

RTRIM(c1,c2])
【功能】删除右边出现的字符串
【参数】C1 字符串
c2 追加字符串,默认为空格
【返回】字符型

 

【相似】LTRIM()删除左边出现的字符串
【相反】RPAD() 在列的右边粘贴字符


替换子串字符 replace(c1,c2[,c3])

REPLACE(c1,c3])
【功能】将字符表达式值中,部分相同字符串,替换成新的字符串
【参数】
c1 希望被替换的字符或变量
c2 被替换的字符串
c3 要替换的字符串,默认为空(即删除之意,不是空格)
【返回】字符型

 

字符串语音表示形式 soundex(c1)

SOUNDEX(c1)
【功能】返回字符串参数的语音表示形式
【参数】c1,字符型
【返回】字符串
【说明】相对于比较一些读音相同,但是拼写不同的单词是非常有用的。

计算语音的算法:
  1.保留字符串首字母,但删除a、e、h、i、o、w、y
  2.将下表中的数字赋给相对应的字母
  (1) 1:b、f、p、v
  (2) 2:c、g、k、q、s、x、z
  (3) 3:d、t
  (4) 4:l
  (5) 5:m、n
  (6) 6:r
  3. 如果字符串中存在拥有相同数字的2个以上(包含2个)的字母在一起(例如b和f),或者只有h或w,则删除其他的,只保留1个
  4.只返回前4个字节,不够用0填充

示例:
  

 
 
 

截取字符串(全角算1字符) substr(c1,n1[,n2])

SUBSTR(c1,n2])
【功能】取子字符串
【说明】多字节符(汉字、全角符等),按1个字符计算
【参数】在字符表达式c1里,从n1开始取n2个字符;若不指定n2,则从第y个字符直到结束的字串.
【返回】字符型

 

截取字符串(全角算2字符) substrb(c1,n2])

SUBSTRB(c1,n2])
【功能】取子字符串
【说明】多字节符(汉字、全角符等),按2个字符计算
【参数】在字符表达式c1里,从n1开始取n2个字符;若不指定n2,则从第y个字符直到结束的字串.
【返回】字符型,如果从多字符右边开始,则用空格表示。

 

替换子字符 translate(c1,c2,c3)

TRANSLATE(c1,c3)
【功能】将字符表达式值中,指定字符替换为新字符
【说明】多字节符(汉字、全角符等),按1个字符计算
【参数】
c1 希望被替换的字符或变量
c2 查询原始的字符集
c3 替换新的字符集,将c2对应顺序字符,替换为c3对应顺序字符
如果c3长度大于c2,则c3长出后面的字符无效
如果c3长度小于c2,则c2长出后面的字符均替换为空(删除)
如果c3长度为0,则返回空字符串。
如果c2里字符重复,按首次位置为替换依据

【返回】字符型

<code class="hljs oxygene has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro",136); box-sizing: border-box;">select</span> TRANSLATE(<span class="hljs-string" style="color: rgb(0,0); box-sizing: border-box;">'he love you'</span>,0); box-sizing: border-box;">'he'</span>,0); box-sizing: border-box;">'i'</span>),102); box-sizing: border-box;">2</span>  TRANSLATE(<span class="hljs-string" style="color: rgb(0,0); box-sizing: border-box;">'重庆的人'</span>,0); box-sizing: border-box;">'重庆的'</span>,0); box-sizing: border-box;">'上海男'</span>),102); box-sizing: border-box;">3</span>  TRANSLATE(<span class="hljs-string" style="color: rgb(0,0); box-sizing: border-box;">'重庆的重庆'</span>,0); box-sizing: border-box;">'北京男士们'</span>),102); box-sizing: border-box;">4</span>  TRANSLATE(<span class="hljs-string" style="color: rgb(0,0); box-sizing: border-box;">'1北京男士们'</span>),102); box-sizing: border-box;">5</span>  TRANSLATE(<span class="hljs-string" style="color: rgb(0,0); box-sizing: border-box;">'1重庆的重庆'</span>,0); box-sizing: border-box;">'北京男士们'</span>) <span class="hljs-keyword" style="color: rgb(0,136); box-sizing: border-box;">from</span> dual;

TRANSLATE(<span class="hljs-string" style="color: rgb(0,0); box-sizing: border-box;">'HELOVEYOU'</span>,0); box-sizing: border-box;">'HE'</span>,0); box-sizing: border-box;">'I'</span> TRANSLATE(<span class="hljs-string" style="color: rgb(0,TRANSLATE(<span class="hljs-string" style="color: rgb(0,0); box-sizing: border-box;">'重庆的重 TRANSLATE('</span>重庆的人<span class="hljs-string" style="color: rgb(0,0); box-sizing: border-box;">','</span>重庆的重 TRANSLATE(<span class="hljs-string" style="color: rgb(0,0); box-sizing: border-box;">'1重庆的?
------------------------------ ------------------------------ ------------------------------ ------------------------------ ------------------------------
i lov you                      上海男人                       北京男人                       1北京人                        京男士人
</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238,221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238,221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li></ul>

删除左边和右边字符串 trim(c1 from c2)

TRIM(c1 from c2)
【功能】删除左边和右边出现的字符串
【参数】C2 删除前字符串
c1 删除字符串,默认为空格
【返回】字符型

 

日期函数

返回系统当前日期 sysydate

sysdate
【功能】:返回当前日期。
【参数】:没有参数,没有括号
【返回】:日期

 

返回指定月数的日期 add_months()

add_months(d1,n1)
【功能】:返回在日期d1基础上再加n1个月后新的日期。
【参数】:d1,日期型,n1数字型
【返回】:日期

 

返回本月最后一天的日期 last_day()

last_day(d1)
【功能】:返回日期d1所在月份最后一天的日期。
【参数】:d1,日期型
【返回】:日期

 

返回两个日期间隔月数 months_between

months_between(d1,d2)
【功能】:返回日期d1到日期d2之间的月数。
【参数】:d1,d2 日期型

【返回】:数字
如果d1>d2,则返回正数
如果d1

 

返回时区的对应时间 new_time()

NEW_TIME(dt1,c1,c2)
【功能】:给出时间dt1在c1时区对应c2时区的日期和时间
【参数】:dt1,d2 日期型

【返回】:日期时间

【参数】:c1,c2对应的 时区及其简写
大西洋标准时间:AST或ADT
阿拉斯加_夏威夷时间:HST或HDT
英国夏令时:BST或BDT
美国山区时间:MST或MDT
美国中央时区:CST或CDT
新大陆标准时间:NST
美国东部时间:EST或EDT
太平洋标准时间:PST或PDT
格林威治标准时间:GMT
Yukou标准时间:YST或YDT

 
 

四舍五入后的日期第一天 round()

round(d1[,c1])
【功能】:给出日期d1按期间(参数c1)四舍五入后的期间的第一天日期(与数值四舍五入意思相近)
【参数】:d1日期型,c1为字符型(参数),c1默认为j(即最近0点日期)
【参数表】:c1对应的参数表:
最近0点日期: 取消参数c1或j
最近的星期日:day或dy或d
最近月初日期:month或mon或mm或rm
最近季日期:q
最近年初日期:syear或year或yyyy或yyy或yy或y(多个y表示精度)
最近世纪初日期:cc或scc

<code class="hljs applescript has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro",monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">SQL> select sysdate 当时日期,102); box-sizing: border-box;">2</span>  <span class="hljs-command" style="box-sizing: border-box;">round</span>(sysdate) 最近<span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">0</span>点日期,102); box-sizing: border-box;">3</span>  <span class="hljs-command" style="box-sizing: border-box;">round</span>(sysdate,'<span class="hljs-property" style="box-sizing: border-box;">day</span>') 最近星期日,102); box-sizing: border-box;">4</span>  <span class="hljs-command" style="box-sizing: border-box;">round</span>(sysdate,'<span class="hljs-property" style="box-sizing: border-box;">month</span>') 最近月初,102); box-sizing: border-box;">5</span>  <span class="hljs-command" style="box-sizing: border-box;">round</span>(sysdate,'q') 最近季初日期,102); box-sizing: border-box;">6</span>  <span class="hljs-command" style="box-sizing: border-box;">round</span>(sysdate,'<span class="hljs-property" style="box-sizing: border-box;">year</span>') 最近年初日期 <span class="hljs-keyword" style="color: rgb(0,136); box-sizing: border-box;">from</span> dual;

当时日期    最近<span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">0</span>点日期 最近星期日  最近月初    最近季初日期 最近年初日期
<span class="hljs-comment" style="color: rgb(136,0); box-sizing: border-box;">----------- ----------- ----------- ----------- ------------ ------------</span>
<span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">25</span>   <span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">22</span>   <span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">6</span>-<span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">1</span>    <span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">7</span>-<span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">1</span>     <span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">1</span>-<span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">1</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238,221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li></ul>

返回日期所在期间的第一天 trunc()

trunc(d1[,c1])
【功能】:返回日期d1所在期间(参数c1)的第一天日期
【参数】:d1日期型,c1为字符型(参数),c1默认为j(即当前日期)
【参数表】:c1对应的参数表:
最近0点日期: 取消参数c1或j
最近的星期日:day或dy或d (每周顺序:日,一,二,三,四,五,六)
最近月初日期:month或mon或mm或rm
最近季日期:q
最近年初日期:syear或year或yyyy或yyy或yy或y(多个y表示精度)
最近世纪初日期:cc或scc

<code class="hljs mel has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro",136); box-sizing: border-box;">select</span> sysdate 当时日期,102); box-sizing: border-box;">2</span>  <span class="hljs-keyword" style="color: rgb(0,136); box-sizing: border-box;">trunc</span>(sysdate) 今天日期,102); box-sizing: border-box;">3</span>  <span class="hljs-keyword" style="color: rgb(0,136); box-sizing: border-box;">trunc</span>(sysdate,0); box-sizing: border-box;">'day'</span>) 本周星期日,102); box-sizing: border-box;">4</span>  <span class="hljs-keyword" style="color: rgb(0,0); box-sizing: border-box;">'month'</span>) 本月初,102); box-sizing: border-box;">5</span>  <span class="hljs-keyword" style="color: rgb(0,0); box-sizing: border-box;">'q'</span>) 本季初日期,102); box-sizing: border-box;">6</span>  <span class="hljs-keyword" style="color: rgb(0,0); box-sizing: border-box;">'year'</span>) 本年初日期 from dual;

当时日期    今天日期    本周星期日  本月初      本季初日期  本年初日期
----------- ----------- ----------- ----------- ----------- -----------
<span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">4</span>-<span class="hljs-number" style="color: rgb(0,221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li></ul>

返回下周某一天的日期 next_day()

next_day(d1[,c1])
【功能】:返回日期d1在下周,星期几(参数c1)的日期
【参数】:d1日期型,c1为字符型(参数),c1默认为j(即当前日期)
【参数表】:c1对应:星期一,星期二,星期三……星期日
【返回】:日期

 

提取时间日期中的数据 extract()

extract(c1 from d1)
【功能】:日期/时间d1中,参数(c1)的值
【参数】:d1日期型(date)/日期时间型(timestamp),c1为字符型(参数)
【参数表】:c1对应的参数表详见示例
【返回】:字符

 
 
 

返回会话中的时间和日期 localtimestamp

localtimestamp
【功能】:返回会话中的日期和时间
【参数】:没有参数,没有括号
【返回】:日期

 

返回当前会话时区中的当前日期和时间 current_timestamp

current_timestamp
【功能】:以timestamp with time zone数据类型返回当前会话时区中的当前日期
【参数】:没有参数,没有括号
【返回】:日期

 

返回数据库时区设置 dbtimezone

dbtimezone
【功能】:返回时区
【参数】:没有参数,没有括号
【返回】:字符型

 

返回当前会话时区 sessiontimezone

SESSIONTIMEZONE
【功能】:返回会话时区
【参数】:没有参数,没有括号
【返回】:字符型

 

变动日期时间数值 interval

INTERVAL c1 set1
【功能】:变动日期时间数值
【参数】:c1为数字字符串或日期时间字符串,set1为日期参数
【参数表】:set1具体参照示例
【返回】:日期时间格式的数值,前面多个+号
以天或天更小单位时可用数值表达式借用,如1表示1天,1/24表示1小时,1/24/60表示1分钟

 

转换函数

字符串转为rowid值 chartorowid(c1)

chartorowid(c1) 。。
【功能】转换varchar2类型为rowid值
【参数】c1,字符串,长度为18的字符串,字符串必须符合rowid格式
【返回】返回rowid值

 

【说明】
在Oracle中,每一条记录都有一个rowid,rowid在整个数据库中是唯一的,rowid确定了每条记录是在Oracle中的哪一个数据文件、块、行上。
在重复的记录中,可能所有列的内容都相同,但rowid不会相同.


rowid的值转换为字符串 rowidtochar(rowid)

ROWIDTOCHAR(rowid) 。。
【功能】转换rowid值为varchar2类型
【参数】rowid,固定参数
【返回】返回长度为18的字符串

 

【说明】
在Oracle中,每一条记录都有一个rowid,rowid在整个数据库中是唯一的,rowid确定了每条记录是在Oracle中的哪一个数据文件、块、行上。
在重复的记录中,可能所有列的内容都相同,但rowid不会相同.


字符串语言字符集转换 convert(c1,set1,set2)

CONVERT(c1,set2)
【功能】将源字符串c1 从一个语言字符集set2转换到另一个目的set1字符集
【参数】c1,字符串,set1,set2为字符型参数
【返回】字符串

 
 

十六进制构成的字符串转换为二进制 HEXTORAW(c1)

HEXTORAW(c1)
【功能】将一个十六进制构成的字符串转换为二进制
【参数】c1,十六进制的字符串
【返回】字符串

 

二进制构成的字符串转换为十六进制 rawtohex(c1)

RAWTOHEX(c1)
【功能】将一个二进制构成的字符串转换为十六进制
【参数】c1,二进制的字符串
【返回】字符串

 

将日期或数据转换为char数据类型TO_CHAR(x[[,c2],C3])

【功能】将日期或数据转换为char数据类型
【参数】
x是一个date或number数据类型。
c2为格式参数
c3为NLS设置参数
如果x为日期nlsparm=NLS_DATE_LANGUAGE 控制返回的月份和日份所使用的语言。
如果x为数字nlsparm=NLS_NUMERIC_CHARACTERS 用来指定小数位和千分位的分隔符,以及货币符号。
NLS_NUMERIC_CHARACTERS =”dg”,NLS_CURRENCY=”string”
【返回】varchar2字符型

【说明1】x为数据型时

<code class="hljs bash has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro",monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">to_char(<span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">1210.73</span>,0); box-sizing: border-box;">'9999.9'</span>) 返回 <span class="hljs-string" style="color: rgb(0,0); box-sizing: border-box;">'1210.7'</span> 
to_char(<span class="hljs-number" style="color: rgb(0,0); box-sizing: border-box;">'9,999.99'</span>) 返回 <span class="hljs-string" style="color: rgb(0,0); box-sizing: border-box;">'1,210.73'</span> 
to_char(<span class="hljs-number" style="color: rgb(0,0); box-sizing: border-box;">'$9,999.00'</span>) 返回 <span class="hljs-string" style="color: rgb(0,0); box-sizing: border-box;">'$1,102); box-sizing: border-box;">21</span>,0); box-sizing: border-box;">'000099'</span>) 返回 <span class="hljs-string" style="color: rgb(0,0); box-sizing: border-box;">'000021'</span> 
to_char(<span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">852</span>,0); box-sizing: border-box;">'xxxx'</span>) 返回<span class="hljs-string" style="color: rgb(0,0); box-sizing: border-box;">' 354'</span>
</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238,221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>

【说明2】x为日期型,c2可用参数

 

【示例】带C3示例

 

字符串转换为日期型 TO_DATE(X[,c3]])

【功能】将字符串X转化为日期型
【参数】c2,c3,字符型,参照to_char()
【返回】字符串

如果x格式为日期型(date)格式时,则相同表达:date x
如果x格式为日期时间型(timestamp)格式时,则相同表达:timestamp x

【相反】 to_char(date[,c3]])

<code class="hljs lasso has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro",monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">SQL<span class="hljs-subst" style="color: rgb(0,0); box-sizing: border-box;">></span> <span class="hljs-keyword" style="color: rgb(0,136); box-sizing: border-box;">select</span> to_date(<span class="hljs-string" style="color: rgb(0,0); box-sizing: border-box;">'199912'</span>,0); box-sizing: border-box;">'yyyymm'</span>),102); box-sizing: border-box;">2</span>  to_date(<span class="hljs-string" style="color: rgb(0,0); box-sizing: border-box;">'2000.05.20'</span>,0); box-sizing: border-box;">'yyyy.mm.dd'</span>),102); box-sizing: border-box;">3</span>  (<span class="hljs-built_in" style="color: rgb(102,102); box-sizing: border-box;">date</span> <span class="hljs-string" style="color: rgb(0,0); box-sizing: border-box;">'2008-12-31'</span>) XXdate,102); box-sizing: border-box;">4</span>  to_date(<span class="hljs-string" style="color: rgb(0,0); box-sizing: border-box;">'2008-12-31 12:31:30'</span>,0); box-sizing: border-box;">'yyyy-mm-dd hh24:mi:ss'</span>),102); box-sizing: border-box;">5</span>  (timestamp <span class="hljs-string" style="color: rgb(0,0); box-sizing: border-box;">'2008-12-31 12:31:30'</span>) XXtimestamp
  <span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">6</span>  from dual;

TO_DATE(<span class="hljs-string" style="color: rgb(0,0); box-sizing: border-box;">'YYYYMM'</span>) TO_DATE(<span class="hljs-string" style="color: rgb(0,0); box-sizing: border-box;">'YYYY.MM. XXDATE TO_DATE('</span><span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">2008</span><span class="hljs-subst" style="color: rgb(0,0); box-sizing: border-box;">-</span><span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">12</span><span class="hljs-subst" style="color: rgb(0,102); box-sizing: border-box;">3112</span>:<span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">31</span>:<span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">30</span><span class="hljs-string" style="color: rgb(0,'</span> XXTIMESTAMP
<span class="hljs-subst" style="color: rgb(0,0); box-sizing: border-box;">--------------------------</span> <span class="hljs-subst" style="color: rgb(0,0); box-sizing: border-box;">------------------------------</span> <span class="hljs-subst" style="color: rgb(0,0); box-sizing: border-box;">-----------</span> <span class="hljs-subst" style="color: rgb(0,0); box-sizing: border-box;">--------------------------------------------------------------------------------</span>
<span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">1999</span><span class="hljs-subst" style="color: rgb(0,102); box-sizing: border-box;">01</span>      <span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">2000</span><span class="hljs-subst" style="color: rgb(0,102); box-sizing: border-box;">05</span><span class="hljs-subst" style="color: rgb(0,102); box-sizing: border-box;">20</span>          <span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">12</span>:<span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">30</span> <span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">31</span><span class="hljs-attribute" style="box-sizing: border-box;">-DEC</span><span class="hljs-subst" style="color: rgb(0,102); box-sizing: border-box;">08</span> <span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">12.31</span><span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">.30</span><span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">.000000000</span> PM
</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238,221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238,221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li></ul>

字符串转换为字符型TO_NUMBER(X[[,c3])

TO_NUMBER(X[[,c3])
【功能】将字符串X转化为数字型
【参数】c2,字符型,参照to_char()
【返回】数字串
【相反】 to_char(date[[,c3])

 
 

半角转化为全角TO_MULTI_BYTE(c1)

TO_MULTI_BYTE(c1)
【功能】将字符串中的半角转化为全角
【参数】c1,字符型
【返回】字符串

 

全角转化为半角to_single_byte(c1)

to_single_byte(c1)
【功能】将字符串中的全角转化为半角
【参数】c1,字符型
【返回】字符串

 

字符集名称转换为ID nls_charset_id(c1)

nls_charset_id(c1)
【功能】返回字符集名称参应id值
【参数】c1,字符型
【返回】数值型

 

字符集ID转换为名称 nls_charset_name(n1)

nls_charset_name(n1)
【功能】返回字符集名称参应id值
【参数】n1,数值型
【返回】字符型

 

聚组函数


统计平均值 AVG([distinct|all]x)

AVG([distinct|all]x)
【功能】统计数据表选中行x列的平均值。
【参数】all表示对所有的值求平均值,distinct只对不同的值求平均值,默认为all
如果有参数distinct或all,需有空格与x(列)隔开。
【参数】x,只能为数值型字段
【返回】数字值

 

统计合计值 SUM([distinct|all]x)

【功能】统计数据表选中行x列的合计值。
【参数】all表示对所有的值求合计值,distinct只对不同的值求合计值,默认为all
如果有参数distinct或all,需有空格与x(列)隔开。
【参数】x,只能为数值型字段
【返回】数字值

 

统计标准误差 STDDEV([distinct|all]x)

【功能】统计数据表选中行x列的标准误差。
【参数】all表示对所有的值求标准误差,distinct只对不同的值求标准误差,默认为all
如果有参数distinct或all,需有空格与x(列)隔开。
【参数】x,只能为数值型字段
【返回】数字值

 

统计方差 VARIANCE([distinct|all]x)

【功能】统计数据表选中行x列的方差。
【参数】all表示对所有的值求方差,distinct只对不同的值求方差,默认为all
如果有参数distinct或all,需有空格与x(列)隔开。
【参数】x,只能为数值型字段
【返回】数字值

 

统计查询所得的行数 count(*|[distinct|all]x)

count(*|[distinct|all]x)
【功能】统计数据表选中行x列的合计值。
【参数】
*表示对满足条件的所有行统计,不管其是否重复或有空值(NULL)
all表示对所有的值统计,默认为all
distinct只对不同的值统计,
如果有参数distinct或all,需有空格与x(列)隔开,均忽略空值(NULL)。
【参数】x,可为数字、字符、日期型及其它类型的字段
【返回】数字值

<code class="hljs sql has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro",monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">【示例】
环境:
<span class="hljs-operator" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0,136); box-sizing: border-box;">create</span> <span class="hljs-keyword" style="color: rgb(0,136); box-sizing: border-box;">table</span> table3(xm <span class="hljs-keyword" style="color: rgb(0,136); box-sizing: border-box;">varchar</span>(<span class="hljs-number" style="color: rgb(0,sal <span class="hljs-keyword" style="color: rgb(0,136); box-sizing: border-box;">number</span>(<span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">2</span>));</span>
<span class="hljs-operator" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0,136); box-sizing: border-box;">insert</span> <span class="hljs-keyword" style="color: rgb(0,136); box-sizing: border-box;">into</span> table3 <span class="hljs-keyword" style="color: rgb(0,136); box-sizing: border-box;">values</span>(<span class="hljs-string" style="color: rgb(0,102); box-sizing: border-box;">1111.11</span>);</span>
<span class="hljs-operator" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0,102); box-sizing: border-box;">5555.55</span>);</span>
<span class="hljs-operator" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0,0); box-sizing: border-box;">''</span>,102); box-sizing: border-box;">0</span>);</span>
</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238,221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li></ul>
 

统计最大值 MAX([distinct|all]x)

【功能】统计数据表选中行x列的最大值。
【参数】all表示对所有的值求最大值,distinct只对不同的值求最大值,默认为all
如果有参数distinct或all,需有空格与x(列)隔开。
【参数】x,可为数字、字符或日期型字段
【返回】对应x字段类型

 
 

统计最小值 MIN([distinct|all]x)

【功能】统计数据表选中行x列的最小值。

【参数】all表示对所有的值求最小值,distinct只对不同的值求最小值,默认为all
如果有参数distinct或all,需有空格与x(列)隔开。

【参数】x,可为数字、字符或日期型字段

【返回】对应x字段类型
注:字符型字段,将忽略空值(NULL)

 
 

其它函数

为空值赋值 nvl() nvl2()

nvl()

【语法】NVL (expr1,expr2) 【功能】若expr1为NULL,返回expr2;expr1不为NULL,返回expr1。
注意两者的类型要一致

nvl2():

【语法】NVL2 (expr1,expr2,expr3)
【功能】expr1不为NULL,返回expr2;expr2为NULL,返回expr3。
expr2和expr3类型不同的话,expr3会转换为expr2的类型

条件取值 decode

decode(条件,值1,翻译值1,值2,翻译值2,…值n,翻译值n,缺省值)

【功能】根据条件返回相应值

【参数】c1,…,cn,字符型/数值型/日期型,必须类型相同或null 注:值1……n
不能为条件表达式,这种情况只能用case when then end解决

·含义解释:  
 

<code class="hljs r has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro",monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;"> decode(条件,值<span class="hljs-number" style="color: rgb(0,翻译值<span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">2</span>,136); box-sizing: border-box;">...</span>值n,缺省值) </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238,221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238,221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

  该函数的含义如下:  
  IF 条件=值1 THEN
  RETURN(翻译值1)
  ELSIF 条件=值2 THEN
  RETURN(翻译值2)
  ……
  ELSIF 条件=值n THEN
  RETURN(翻译值n)  
  ELSE
  RETURN(缺省值)
  END IF
或者:
 when case 条件=值1 THEN
  RETURN(翻译值1)
  ElseCase 条件=值2 THEN
  RETURN(翻译值2)
  ……
  ElseCase 条件=值n THEN
  RETURN(翻译值n)  
  ELSE
  RETURN(缺省值)
  END

【示例】
  ·使用方法:  
  1、比较大小  
  select decode(sign(变量1-变量2),-1,变量1,变量2) from dual; –取较小值
  sign()函数根据某个值是0、正数还是负数,分别返回0、1、-1  
  例如:
  变量1=10,变量2=20
  则sign(变量1-变量2)返回-1,decode解码结果为“变量1”,达到了取较小值的目的。
  
  2、表、视图结构转化  
  现有一个商品销售表sale,表结构为:  
 

<code class="hljs livecodeserver has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro",monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;"> month    <span class="hljs-keyword" style="color: rgb(0,136); box-sizing: border-box;">char</span>(<span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">6</span>)      <span class="hljs-comment" style="color: rgb(136,0); box-sizing: border-box;">--月份</span>
  sell    <span class="hljs-built_in" style="color: rgb(102,102); box-sizing: border-box;">number</span>(<span class="hljs-number" style="color: rgb(0,102); box-sizing: border-box;">10</span>,102); box-sizing: border-box;">2</span>)   <span class="hljs-comment" style="color: rgb(136,0); box-sizing: border-box;">--月销售金额 </span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238,221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238,221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

 
  现有数据为:  
 

<code class="hljs  has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro",monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;"> 200001  1000
  200002  1100
  200003  1200
  200004  1300
  200005  1400
  200006  1500
  200007  1600
  200101  1100
  200202  1200
  200301  1300</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238,221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li></ul>

  
  想要转化为以下结构的数据:  

 

  
  结构转化的SQL语句为:
  
 

                        

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读