07.Oracle中的内置函数

Oracle 内置函数

字符串

  • ascii 得到一个字符的ASCII码(整数)
select ascii('A'), ascii('a'), ascii('0') from dual;
-- 65 97 48
  • chr 根据ASCII码得到字符
select chr(48), chr(68) from dual;
-- 0 D
  • length 返回字符串的字符数
select length('我们hello') from dual;
-- 7
  • vsize 返回字符串的字节数
select vsize('我们hello') from dual;
  • initcap 将英语句子中每个单词的首字母大写(与中文无关)
select initcap('hello world oracle') from dual;
  • lower 转小写
  • upper 转大写
select upper('Hello'), lower('Hello') from dual;
-- HELLO hello
  • substr 字符串截取
select substr('helloworld', 6) from dual;
-- world
select substr('helloworld', 6, 2) from dual;
-- wo
select substr('helloworld', 1, 5) from dual;
-- hello
select substr('helloworld', 0, 5) from dual;
-- hello
select substr('helloworld', -5, 2) from dual;   -- 倒数第5个,截取2个
-- wo
  • instr 查找字符串出现的位置
select instr('helloworld', 'world') from dual;
-- 6
select instr('helloworld', 'World') from dual;  -- 找不到返回0
-- 0
select instr('helloworld', 'l', 5) from dual;   -- 从指定位置开始找
-- 9
  • concat 字符串拼接
select concat('123', '456') from dual;
-- 123456
select '123' || '456' from dual;    -- 管道符
-- 123456

select concat(concat('123', '456'), '789') from dual;
-- 123456789
select '123' || '456' || '789' from dual;
-- 123456789
  • trim/ltrim/rtrim 去除空格(或者指定两侧的字符)
select '[' || trim('   aaa    ') || ']' from dual;
-- [aaa]
select '[' || ltrim('   aaa    ') || ']' from dual;
-- [aaa    ]
select '[' || rtrim('   aaa    ') || ']' from dual;
-- [   aaa]
select '[' || ltrim('12122aaa3434', '123') || ']' from dual;
-- [aaa3434]        指定去除左侧一组出现的字符
select '[' || rtrim('12122aaa3434', '345') || ']' from dual;
-- [12122aaa]       指定去除左侧一组出现的字符
  • lpad/rpad 左补齐/右补齐
select '[' || lpad('hello', 10) || ']' from dual;
-- [     hello]
select '[' || rpad('hello', 10) || ']' from dual;
-- [hello     ]

-- 13813813838
select rpad(substr('13813813838', 1, 7), 11, '*') from dual;
-- 1381381****
  • replace 字符串替换
select replace('helloworld', 'l', 'L') from dual;
-- heLLoworLd
select replace('helloworld', 'll', '*') from dual;
-- he*oworld

数学

  • sign 获取数值的符号位(>0返回1,=0返回0,<0返回-1)
select sign(-0.001), sign(0), sign(0.001) from dual;
-- -1 0 1
  • mod 模
select mod(10, 3), mod(-10, 3), mod(10, -3) from dual;
-- 1 -1 1	最后的数值结果符号与被除数一致
  • abs 绝对值
  • power 幂
select power(2, 2), power(2, -1), power(10, -6) from dual;
-- 4 0.5 0.000001
  • sqrt 平方根
select sqrt(10) from dual;
-- 3.16222
  • ceil 求大于这个数的最小/最近整数

  • floor 求小于这个数的最大/最近整数

select ceil(9.1) from dual;
-- 10
select floor(9.1) from dual;
-- 9
  • round 四舍五入
select round(4.56), round(4.56, 1) from dual;
-- 5 4.6

-- 整数进位
select round(34567.99, -3), round(34567.99, -4) from dual;
-- 35000 30000
  • trunc 截位
select trunc(4.56), trunc(4.56, 1) from dual;
-- 4 4.5
select trunc(34567.99, -3), trunc(39999.99, -4) from dual;
-- 34000 30000

时间日期

与数值进行±运算时,是进行天数的增加和减少

select sysdate - 5 as "五天前", sysdate + 5 as "五天后" from dual;
  • to_char 日期时间转为指定的字符串格式

格式掩码:

yyyy 四位年份

yy 两位年份

mm 两位月份

dd 两位日期

hh 12进制小时

hh24 24进制小时

mi 分钟

ss 秒

ff3 毫秒

day 星期几

d 星期几对应的数值(星期天是1~星期六是7)

select to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss') from dual;
-- 2020-06-10 15:52:40

select to_char(systimestamp, 'yyyy-mm-dd hh24:mi:ss.ff3') from dual;
-- 2020-06-10 15:53:22.38100

select to_char(sysdate, 'yyyy-mm-dd day') from dual;
-- 2020-06-10 星期三

select to_char(sysdate, 'd') from dual;
-- 4

中文时间

select to_char(sysdate,
      'yyyy"年"mm"月"dd"日" hh24"时"mi"分"ss"秒"') from dual;
  • to_date 字符串转为指定的日期时间格式

掩码与to_char相同

select to_date('06/10/2020 16:01', 'mm/dd/yyyy hh24:mi') from dual;

select to_timestamp('06/10/2020 16:01:30.123',
                    'mm/dd/yyyy hh24:mi:ss.ff3') from dual;

  • extract 截取时间日期中的指定部分(年、月、日、时、分、秒)
select
    extract(year from sysdate),
    extract(month from sysdate),
    extract(day from sysdate) from dual;
-- 2020 6 10

-- 时、分、秒,必须通过systimestamp截取
-- 时是没有时区(缺8小时)
select
    extract(hour from systimestamp),
    extract(minute from systimestamp),
    extract(second from systimestamp) from dual;

-- 推荐以后使用to_char截取所需时间部分
select to_char(sysdate, 'hh24') from dual;

求2020-12-25圣诞节是星期几

select to_char(to_date('2020-12-25', 'yyyy-mm-dd'), 'day') from dual;
extract(minute from systimestamp),
extract(second from systimestamp) from dual;

– 推荐以后使用to_char截取所需时间部分
select to_char(sysdate, ‘hh24’) from dual;


求2020-12-25圣诞节是星期几

```sql
select to_char(to_date('2020-12-25', 'yyyy-mm-dd'), 'day') from dual;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值