Oracle常用Sql函数学习

  1. 数值型常用函数
函数返回值样例显示
ceil(n)大于或等于数值n的最小整数select ceil(10.6) from dual;11
floor(n)小于等于数值n的最大整数select ceil(10.6) from dual;10
mod(m,n)m除以n的余数,若n=0,则返回mselect mod(7,5) from dual;2
power(m,n)m的n次方select power(3,2) from dual;9
round(n,m)将n四舍五入,保留小数点后m位select round(1234.5678,2) from dual;1234.57
sign(n)若n=0,则返回0,否则,n>0,则返回1,n<0,则返回-1select sign(12) from dual;1
sqrt(n)n的平方根select sqrt(25) from dual;5
  1. 常用字符处理函数
函数返回值样例显示
initcap(char)把每个字符串的第一个字符换成大写select initicap('mr.ecop') from dual;Mr.Ecop
lower(char)整个字符串换成小写select lower('MR.ecop') from dual;mr.ecop
replace(char,str1,str2)字符串中所有str1换成str2select replace('Scott','s','Boy') from dual;Boycott
substr(char,m,n)取出从m字符开始的n个字符的子串select substr('ABCDEF',2,2) from dual;CD
length(char)求字符串的长度select length('ACD') from dual;3
||并置运算符(连接字符串)select 'ABCD'||'EFGH' from dual;ABCDEFGH
to_number()将合法的数字字符串转换成数字类型select to_number('88877') from dual;88877
to_char()将数字转换为字符串select to_char(88877) from dual;'88877'
CONCAT (char1, char2)返回连接“char1”和“char2”SELECT CONCAT( CONCAT('lisa', ' is a '), 'PM') FROM dual;lisa is a PM
LPAD(char1,n [,char2])返回“char1”,左起由“char2”中的字符补充到“n”个字符长。如果“char1”比“n”长,则函数返回“char1”的前“n”个字符SELECT LPAD('abcdef',10,'*') FROM dual;****abcdef
LTRIM(string,trim_set)从左边删除字符,此处“string”是数据库的列,或者是字面字符串,而“trim_set” 是我们要去掉的字符的集合SELECT LTRIM('abcdab','a') FROM DUAL;bcdab
RPAD(char1, n [,char2])返回“char1”,右侧用“char2”中的字符补充到“n”个字符长。如果 “char1”比“n” 长,则函数返回“char1”的前“n”个字符SELECT RPAD('abcdef',10,'*') FROM emp;abcdef****
RTRIM(string,trim_set)从右侧删除字符,此处“string”是数据库的列,或者是字面字符串,而“trim_set” 是我们要去掉的字符的集合SELECT RTRIM('abcdef', 'f') FROM DUAL;abcde
TRANSLATE(string, if, then)“if”中字符的位置,并检查“then”的相同位置,然后用该位置的字符替换 “string”中的字符SELECT TRANSLATE('abcdef','cd', 'xx') FROM dual;abxxef
UPPER(string)返回大写的“string”SELECT UPPER('sql') FROM dual;SQL
ASCII(string)该命令是“American Standard Code for Information Interchange”的缩写。它是使用数字表示可打印字符的基本规则。该函数返回 “string”中第一个(最左边)字符的 ASCII 值SELECT ASCII('APTECH') from dual;65
INSTR (string, set[, start[, occurrence] ] )该命令“string”中从“start”位置开始查找字符集合的位置,再查找“set”出现的第一次、第二次等等的“occurrence”(次数)。“start”的值也可以是负数,代表从字符串结尾开始向反方向搜索。该函数也用于数字和日期数据类型SELECT INSTR('aptech is aptech','ap',1,2) FROM DUAL;11
  1. 日期型函数
函数返回值样例显示
sysdate当前日期和时间select sysdate from dual;2019/11/01 15:20:25
last_day本月最后一天select last_day(sysdate) from dual;2019/11/30 15:20:25
add_months(d,n)当前日期d后推n个月select add_months(sysdate,2) from dual;2020/1/1 15:20:25
months_between(d,n)日期d和n相差月数select months_between(sysdate,to_date('20190801','YYYYMMDD')) from dual;3
next_day(d,day)d后第一周指定day的日期select next_day(sysdate,1) from dual;2019/11/03 15:20:25

next_day(d,day)函数中day的值:‘1’ 星期一;‘2’ 星期二;‘3’ 星期三;‘4’ 星期四;‘5’ 星期五;‘6’ 星期六;‘7’ 星期日

  1. 特殊格式的日期型函数
函数返回值样例显示
Y或YY或YYY年的最后一位,两位,三位select to_char(sysdate,'YYY') from dual;019
Q季度,1-3月为第一季度select to_char(sysdate,'Q') from dual;4
MM月份数select to_char(sysdate,'MM') from dual;11
RM月份的罗马表示select to_char(sysdate,'RM') from dual;XI
month用9个字符表示的月份名select to_char(sysdate,'month') from dual;11月
ww当年第几周select to_char(sysdate,'ww') from dual;44
w本月第几周select to_char(sysdate,'w') from dual;1
DDD当年第几天,一月一日为001 ,二月一日032select to_char(sysdate,'DDD') from dual;305
DD当月第几天select to_char(sysdate,'DD') from dual;01
D周内第几天select to_char(sysdate,'D') from dual;6
DY周内第几天缩写select to_char(sysdate,'DY') from dual;星期五
hh1212小时制小时数select to_char(sysdate,'hh12') from dual;03
hh2424小时制小时数select to_char(sysdate,'hh24') from dual;15
Mi分钟数select to_char(sysdate,'Mi') from dual;20
ss秒数select to_char(sysdate,'ss') from dual;25
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值