oracle中的一些常用函数及示例


本文整理自网络,侵删。

前言

学习oracle也有一段时间了,发现oracle中的函数好多,对于做后台的程序猿来说,大把大把的时间还要学习很多其他的新东西,再把这些函数也都记住是不太现实的,所以总结了一下oracle中的一些常用函数及示例,一是为了和大家分享,二是可以在以后工作中忘记了随时查阅。废话不多说,下面直接上函数。

一.单行函数

只处理单个行,并且为每行返回一个结果。

1.字符函数

(1)concat(str1,str2)字符串拼接函数

select concat('Hello ','World') from dual;
--等价于
select 'Hello '||'World' from dual;

(2)initcap(str)将每个单词首字母大写,其他字母小写

select initcap('hello world!') from dual; --返回结果为'Hello World!'
select initcap('HELLO WORLD!') from dual; --返回结果为'Hello World!'

(3)instr(x,find_string[,start][,occurrence])返回指定字符串在某字符串中的位置,可以指定搜索的开始位置和返回第几次搜索出来的结果

----------搜索时下标从1开始计算
select instr('Hello World!','o') from dual;--从1位置开始搜索,返回第一次出现的o的位置,结果为5
select instr('Hello World!','o',6) from dual;--从6位置开始搜索,返回第一次出现的o的位置,结果为8
select instr('Hello World!','o',1,2) from dual;--从1位置开始搜索,返回第二次出现o的位置,结果为8

(4)length(str)返回表达式中的字符数

select length('Hello World!') from dual;--返回结果为12
select length('张三') from dual;--返回结果为2

(5)lower(str)将字符串转换为小写

select lower('Hello World!') from dual;

(6)lengthb(str)返回表达式中的字节数

select lengthb('Hello World!') from dual;--返回结果为12
select lengthb('张三') from dual;--返回结果为6

(7)upper(str)将字符串转换为大写

select upper('Hello World!') from dual;

(8)lpad(str,width[,pad_string])当字符串长度不够时,左填充补齐,可以指定补齐时用什么字符补齐,若不指定,则以空格补齐

select lpad('Hello World!',20) from dual;--返回结果为' Hello World!'
select lpad('Hello World!',20,'*') from dual;--返回结果为'********Hello World!'

(9)rpad(str,width[,pad_string])当字符串长度不够时,右填充补齐,原理同左填充

select rpad('Hello World!',20) from dual;--返回结果为'Hello World! '
select rpad('Hello World!',20,'*+') from dual;--返回结果为'Hello World!*+*+*+*+'

(10)ltrim(x[,trim_string])从字符串左侧去除指定的所有字符串,若没有指定去除的字符串,则默认去除左侧空白符

select ltrim(' Hello World! ') from dual;--返回结果为'Hello World! '
select ltrim('***+*Hello World!***+*','*+') from dual;--返回结果为'Hello World!***+*'

(11)rtrim(x[,trim_string])从字符串右侧去除指定的所有字符串,原理同ltrim()

select rtrim(' Hello World! ') from dual;--返回结果为' Hello World!'
select rtrim('***+*Hello World!***+*','*+') from dual;--返回结果为'***+*Hello World!'

(12)trim(trim_string from x)从字符串两侧去除指定的所有字符串  注意,ltrim()和rtrim()的截取集可以使多个字符,但trim的截取集只能有一个字符

select trim('*+' from '***+*Hello World!***+*') from dual;

(13)nvl(x,value)将一个NULL转换为另外一个值,如果x为NULL,则返回value,否则返回x值本身

insert into student values(7,'猪猪',default,NULL);
select nvl(address,'北京市') from student;

(14)nvl2(x,value1,value2),如果x不为NULL,返回value1,否则,返回value2

select nvl2(address,'有地址','无地址') from student;

(15)replace(x,search_string,replace_string),从字符串x中搜索search_string字符串,并使用replace_string字符串替换。并不会修改数据库中原始值

select replace('Hello World!','o','HA') from dual;

(16)substr(x,start[,length])返回字符串中的指定的字符,这些字符从字符串的第start个位置开始,长度为length个字符;如果start是负数,则从x字符串的末尾开始算起;如果       length省略,则将返回一直到字符串末尾的所有字符

select substr('Hello World',3) from dual; --返回结果为'llo World'
select substr('Hello World',-3) from dual;--返回结果为'rld'
select substr('Hello World',3,2) from dual;--返回结果为'll'
select substr('Hello World',-7,4) from dual;--返回结果为'o Wo'

2.数值函数

阅读剩余部分

相关阅读 >>

oracle 10g安装配置方法图文教程

oracle显示游标的使用及游标for循环

oracle 报警日志如何查看?

db2和 oracle的并发控制(锁)的比较

oracle 如何导入 excel 数据?

oracle用什么sql语句判断表存不存在

oracle开发之报表函数

分享oracle 11g client 客户端安装步骤(图文详解)

oracle11g数据库win8.1系统安装配置图文教程

orace查询数据出现乱码的问题解决思路

更多相关阅读请进入《oracle》频道 >>


数据库系统概念 第6版
书籍

数据库系统概念 第6版

机械工业出版社

本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...