时间:2023-10-31来源:系统城装机大师作者:佚名
1 2 3 4 |
---1. 把小写的字符转换成大小的字符 select upper ( 'yes' ) from dual; 结果:YES ---2. 把大写字符变成小写字符 select lower ( 'YES' ) from dual; 结果:yes |
1 | select CONCAT( 'abc' , 'efg' ) from dual; 结果:abcefg |
1 2 3 4 |
--ASCII(x)返回字符X 的ASCII码 select ASCII( 'a' ) from dual; 结果:97 --CHR(x)接收一个ASCII码 返回一个字符 select CHR(65) from dual; 结果:A |
索引从1开始
1 | select INSTR( 'abcefg123' , '123' ) from dual; 结果:7 |
1 | select LENGTH( 'abcefg123' ) from dual; 结果:9 |
1 2 |
--REPLACE(X,old,new)在X中查找old,并替换成new select REPLACE ( 'abc882388' , '88' , '666' ) from dual; 结果:abc66623666 |
1.6字符截取:substr(str,start,lenth)
1 | --REPLACE(X,old,new)在X中查找old,并替换成new--返回X的字串,从start处开始,截取length个字符,缺省length,默认到结尾select SUBSTR('abcdefg', 4, length('abcdefg')) from dual;结果:defg |
1 2 3 4 5 6 7 8 |
---1. 四舍五入函数:ROUND() ---默认情况下 ROUND 四舍五入取整,可以自己指定保留的位数。 ---保留位数为负数,即往前保留几位数 select round(26.16 ) from dual; 结果:26 select round(26.16, 1) from dual; 结果:26.1 select round(26.16, -1) from dual; 结果:30 select round(26.16, -2) from dual; 结果:0 select round(56.16, -2) from dual; 结果:100 |
1 2 3 4 |
---直接截取,不在看后面位数的数字是否大于5. select trunc(56.56 ) from dual; 结果:56 select trunc(56.16, 1) from dual; 结果:56.1 select trunc(56.16, -1) from dual; 结果:50 |
1 2 |
---求余数 select mod(10, 3) from dual; 结果:1 |
1 | select abs (-1) as a from dual; 结果:1 |
1 | select CEIL(3.00001) as a from dual; 结果:4 |
1 | select FLOOR(2.9999999) as a from dual; 结果:2 |
1 | select POWER(3,4) as a from dual; 结果:81 |
Oracle 中提供了很多和日期相关的函数,包括日期的加减,在日期加减时有一些规律
日期 – 数字 = 日期
日期 + 数字 = 日期
日期 – + 日期 = 数字 (可直接加减,单位是天)
1 2 3 4 5 6 7 8 9 10 |
----查询出emp表中所有员工入职距离现在几天。 select sysdate-e.hiredate from emp e; 结果:以天为单位的小数 ----算出明天此刻 select sysdate+1 from dual; 结果:2020/12/4 16:35:36 ----查询出emp表中所有员工入职距离现在几月。 select months_between(sysdate,e.hiredate) from emp e; 结果:以月为单位的小数 ----查询出emp表中所有员工入职距离现在几年。 select months_between(sysdate,e.hiredate)/12 from emp e; 结果:以年为单位的小数 ----查询出emp表中所有员工入职距离现在几周。 select round((sysdate-e.hiredate)/7) from emp e; 结果:以周为单位的小数 |
1 2 3 4 |
---日期转字符串 select to_char(sysdate, 'yyyy-mm-dd hh:mi:ss' ) from dual; 结果:2020-12-04 05:18:03 12小时有0 select to_char(sysdate, 'fm yyyy-mm-dd hh:mi:ss' ) from dual; 结果:2020-12-4 5:18:32 12小时无0 select to_char(sysdate, 'fm yyyy-mm-dd hh24:mi:ss' ) from dual; 结果: 2020-12-4 17:18:47 24小时无0 |
1 2 3 4 5 6 |
可以使用 TO_CHAR 函数来拆分年月日 select empno,ename to_char(hiredate, 'yyyy' ) 年, to_char(hiredate, 'mm' ) 月, to_char(hiredate, 'dd' ) 日 from emp; |
1 2 3 |
select empno,ename to_char(hiredate, 'yyyy-mm-dd' ) 日 from emp; |
to_char(number,‘L999,999.99’)
L代表货币符号,代表千位数分隔符.代表小数位分隔符
1 | select to_char(123456.99, 'L999,999.99' ) from dual; 结果: $123,456.99 |
1 | select to_date( '2020-12-4 17:18:47' , 'fm yyyy-mm-dd hh24:mi:ss' ) from dual; |
1 2 3 4 5 6 |
---算出emp表中所有员工的年薪(奖金+12个月 月薪) select e.sal*12 from emp e; 结果:12个月 月薪 ---奖金里面有null值,如果null值和任意数字做算术运算,结果都是null。 select e.sal*12+e.comm from emp e; 结果:有员工结果为空 ---如果 e.comm 为空,则正常相加;e.comm 为 null,则用 0取代 e.comm select e.sal*12+nvl(e.comm, 0) from emp e; 结果:正常查询员工年薪 |
到此这篇关于Oracle单行函数(字符,数值,日期,转换)的文章就介绍到这了
2023-10-31
ORACLE创建DBlink的过程及使用方法2023-10-31
Oracle数据库启停命令示例2023-10-31
Oracle建表与创建序列详细实例Oracle rac环境的数据库导入操作 以下应是规范的操作步骤: 1. 创建表空间前,先查看表空间所在目录 2. 创建用户表空间、用户及密码 3. 如果表空间不足,需创建足够的表空间文件 4. 如果上述创建的表空间存在问题,执行删除...
2023-10-31
一、为什么要分区(Partition) 二、oracle 11g 如何按天、周、月、年自动分区 2.1 按年创建 2.2 按月创建 2.3 按天创建 2.4 按周创建 2.5 测试 三、numtoyminterval 和 numtodsinterval 的区别 3.1 numtodsinterval(<x...
2023-10-31