【Oracle】内置函数

132 阅读3分钟

字符处理函数

// 截取,截取方向始终向右
// pos-开始位置,可为负值
substr(str, pos[, len])
​
upper(str)  // 转大写
lower(str)  // 转小写
initcap(str)    // 首字母大写
length(str)
concat(a, b)    // 拼接,类似 ||
trim(str)   // 去除前后空格// 查找,找不到返回`0`
// str2-查找字符,pos-开始位置,n-第几个,故`instr(str1, str2)`等价于`instr(str1, str2, 1, 1)`
instr(str1, str2[, pos][, n])
​
// 将`str1`用`str2`向左 / 向右填充长度至`len`
lpad/rpad(str1, len, str2)
​
// 将`str1`中的`str2`用`str3`替换
replace(str1, str2, str3)
​
// 去除 b 中开头 / 结尾 / 开头和结尾 / 开头以及结尾的 a
trim(leading/trailing/both/无 a from b)

字符串前有一个' '(空字符),故首字符的位置是1,不是0

数字处理函数

// 四舍五入
// b-精确位数,若`b < 0`,则向左精确,故`round(a)`等价于`round(a, 0)`
round(a, b)
​
mod(a, b)   // 等同于`a%b`,余数符号跟 a
​
trunc(a, b)     // 数值取整。以`10`为整。b-精确位数

非空判断函数

// 若`a`为`null`,返回 b,否则返回 a
nvl(a, b)
​
// 若`a`不为`null`,返回 b,否则返回 c
nvl2(a, b, c)
​
// 比较 ab,若`a = b`,返回`null`,否则返回 a
nullif(a, b)
​
// 若`xx = w1`或`w1`为`true`,返回 c1;若`xx = w2`或`w2`为`true`,返回 c2;否则返回 c3
case xx when w1 then c1 when w2 then c2 else c3 end
​
// 作用同`case()
decode(xx, w1, c1, w2, c2, c3)

日期函数

// 返回`d1``d2`相差的自然月数
months_between(d1, d2)
​
// 增加月数
add_months(d, n)
​
// 返回 d 后的第1个星期一
next_day(d, '星期一')
​
// 返回 d 当月的最后1天
last_day(d)
​
// 以`day`四舍五入。
// 'dd'`是格式码,其他格式码:`'CC'`→ 世纪,`'YY'`→ 年,`'mm'`→ 月,`'hh24'`→ 小时,`'mi'`→ 分钟,`'ss'`→ 秒。其中,`round(d, 'dd')`等价于`round(d)``
round(d, 'dd')
​
// 同`round()``trunc(d, 'dd')`等价于`trunc(d)`
trunc(d, 'dd')
​
// 返回 d 的天数。
// `day`是标识符,表示“天”。其他标识符:`'year'`→ 年,`'month'`→ 月,
extract(day from d)

正则表达式相关函数

// 类似`like`,判断是否包含匹配模式`pattern`的字符串,故仅能用于进行判断的位置,如:`where`、`check()`
regexp_like(a, pattern)
​
// 类似`substr()`
// n-第几个
regexp_substr(a, pattern, pos, n)
​
// 类似`instr()`
regexp_instr(a, pattern, pos, n)
​
// 统计匹配模式的字符串个数
regexp_count(a, pattern)
​
// 类似`replace()`
regexp_replace(a, pattern, b)

注:

  1. 这些函数的参数列表是“必须”部分,考虑到实用性,一些可选参数未列举出。
  2. “模式”即正则表达式,如果大家不了解,可阅读博文《正则表达式全解析+常用示例》(转发);若想深入学习,请阅读# Pattern类中的【正则表达式的构造摘要】一栏。

其他函数

sysdate     // 返回系统时间

本文持续更新中。。。