1. 字符函数

--upper(str), lower(str):将str转换成大,小写
select upper('hello') as x1, lower('HELlo') as x2 from dual;
--initcap(str):将str中的每个单词的首字母大写,其他字母小写
select initcap('hELlo world!') as x from dual;
--concat(str1, str2)或(str1 || str2):将str1和str2两个字符串连接成一个字符串
select concat(1 , 'b') as x1, 1.2 || 'b' as x2 from dual;
--length(str):计算str的长度
select length('abcde') as x from dual;
--replace(str, oldSubStr, newSubStr):将str中的字符串oldSubStr替换成newSubStr
select replace('abceda', 'abc', '*') as x from dual;
--lpad(str, lengthAffterPadding, paddingStr):如果length(str)>lengthAffterPadding, 则会截掉右侧的字符串,
--如果length(str)<lengthAffterPadding, 则会在str的左侧用paddingStr进行填充
select lpad('smith111111111', 10, '*') as longStr, lpad('smith', 10, '*1') as shotStr from dual;
--lpad(str, lengthAffterPadding, paddingStr):如果length(str)>lengthAffterPadding, 则会截掉右侧的字符串,
--如果length(str)<lengthAffterPadding, 则会在str的右侧用paddingStr进行填充
select rpad('smith111111111', 10, '*') as longStr, rpad('smith', 10, '*1') as shotStr from dual;
--ascii(character):返回与character字符对应的十进制数
select ascii('A') A,ascii('a') a,ascii('') 一打,ascii(' ') kg from dual;
--chr(num):返回与num十进制数对应的字符
select chr(65) A,chr(122) z from dual;
--chr(10):换行,在command中可以显示出来
select '' || chr(10) || '' || chr(10) || '' from dual;
--trim(str):过滤掉str首尾空格字符,ltrim过滤掉左边的,rtrim过滤掉右边的
select trim(' smith ') as delBoth, ltrim(' smith ') as delL, rtrim(' smith ') as delR from dual;
--instr(str1, str2, startPos, nthAppear):从str1的startPos处开始查找str2的第nthAppear出现的索引(字符串索引从1开始)
select instr('Hello World','or', 7, 1) as res from dual;
--substr(str, start, length):从str的start处开始向右截取length个字符串,没有length值则截取到最后,如果start负,则start是从右到左数start个
select substr('abced', 2, 3) as positivePos, substr('abced', -3, 3) as negativePos from dual;

2. 数值函数

--round(num, digits):将num进行四舍五入,保留digits位小数,如果digits为负数,则对小数点左侧前digitis位进行四舍五入
select round(412.313, 2) as affterDots, round(4521, -2) as beforeDots from dual;
--trunc(num, digits):截取,与round相似,但是不对指定小数前或后的位数进行四舍五入处理
select trunc(412.313, 2) as affterDots, trunc(4521, -2) as beforeDots from dual;
--mod(num, dividend):num%dividend取余,结果的符号与num的符号一直,dividend除数为0的话结果等于num
select mod(3, -2) as positiveNum, mod(-3, -2) as negativeNum, mod(-3, 0) as dividendZero from dual;

3. 日期函数

--返回当前日期
select sysdate from dual;
--返回当前星期第一天
select trunc(sysdate,'dd') from dual;
--返回当月第一天
select trunc(sysdate,'mm') from dual;
--返回当月最后一天
select trunc(last_day(sysdate)) from dual;
--获取当月的天数
select cast(to_char(last_day (sysdate), 'dd') as int) daysOfMonth from dual;
--获取当月剩余天数
select sysdate, last_day (sysdate) "Last day", last_day (sysdate) - sysdate "Days left" from dual;
--系统日期所在月份的倒数第3天
select last_day(sysdate)-2 from dual;
--下个月的今天
select add_months(sysdate,1) from dual;
--返回当年第一天'y'、'yyyy'
select trunc(sysdate,'yyyy') from dual;
--返回一年的最后一天
select add_months(trunc(sysdate,'y'), 12) - 1 from dual
--返回一年的天数(去年的第一天-今年的第一天就是今年的天数)
select add_months(trunc(sysdate,'y'), 12) - trunc(sysdate,'y') from dual;
--从系统日期开始的第一个星期一
select next_day(sysdate,'星期一') from dual;
--months_between(startMonth, endMonth):返回startMonth与endMonth日期之间相隔的月数,若startMonth比endMonth小,则返回一个负数
select months_between(to_date('', 'yyyymmdd'), to_date('', 'yyyymmdd')) as months from dual;
--当前时间减去7秒的时间
select sysdate,sysdate - interval '' second from dual;
--当前时间减去7分的时间
select sysdate,sysdate - interval '' minute from dual;
--当前时间减去7小时的时间
select sysdate,sysdate - interval '' hour from dual;
--当前时间减去7天的时间
select sysdate,sysdate - interval '' day from dual;
--当前时间减去7月的时间
select sysdate,sysdate - interval '' month from dual;
--当前时间减去7年的时间
select sysdate,sysdate - interval '' year from dual;

4. 转换函数

--格式化日期-输出年分
select to_char(sysdate,'yyyy') from dual;
--格式化日期-输出年月日
select to_char(sysdate,'fmyyyy-mm-dd') from dual;
--格式化日期-输出年月日时分秒
select to_char(sysdate,'YYYY/MM/DD HH24:MI:SS') from dual t;
--将字符串转换成指定格式的日期
Select to_date('','yyyyMMdd') from dual;
--将整数转换成指定格式的日期
select to_date(to_char(20141201), 'YYYYMMDD') from dual;
--将字符串转换成指定格式的日期
select to_date('20140630 18:28:20','YYYY/MM/DD HH24:MI:SS') from dual t;
--Oracle中取毫秒级时间(获取的时间是精确到毫秒以后三位的)
select to_char(current_timestamp,'yyyy/mm/dd/hh24:mi:ss:ff') from dual
--格式化-输出美元符号
select to_char(10000000000,'$999,999,999,99') from dual;-- $100,000,000,00
--格式化-输出人民币符号
select to_char(1000000,'L99,999,99') from dual;--输出 ¥10,000,00
--将字符串转换成数字
select to_number('13.2')+to_number('') from dual;
--保留三位有效数字
select trunc(to_number('1000000.123'),3) from dual;

5. 通用函数

--nvl(isnull, default):如果isnull为空则返回default,否则返回isnull
select nvl(null, -1) as resDefault, nvl(2, -1) as resIsNull from dual;
--nullif(exp1, exp2), 如果exp1和exp2相等则返回null,否则返回exp1
select nullif(1, 1) as equalRet, nullif(1, 21) as notEqualRet from dual;
--nvl2(num, notNullRet, nullRet):如果num不为null则返回notNullRet,如果num为null则返回nullRet
select nvl2(1, 1, 2) as notNullRet, nvl2(null, 1, 2) as nullRet from dual;
--coalesce(num1, num2, num3...):返回第一个非null值
select coalesce(null, null, 1) as x from dual;
--decode(num, num1, num1Ret, num2, num2Ret, defaultValue):和case 表达式类似,decode()函数也用于实现多路分支结构
--判断num的值,如果等于num1则返回num1Ret,如果等于num2则返回num2Ret,都不等于则返回defaultValue
select decode(1, 1, 'hi', 2, 'hello', 'defaultValue') from dual;

[02] Oracle简单单行函数(字符+数值+日期+转换+通用)的更多相关文章

  1. SQL入门(2): Oracle内置函数-字符/数值/日期/转换/NVL/分析函数与窗口函数/case_decode

    本文介绍Oracle 的内置函数. 常用!  一. 字符函数 ASCII 码与字符的转化函数 chr(n)   例如 select chr(65) || chr(66) || chr(67) , ch ...

  2. Oracle常用单行函数(原创)

    前言: 想把单行函数进行一个比较全面的总结,并分享给有需要的人,有不明之处还请多多指教. SQL函数:Oracle的内置函数,包括了单行函数和多行函数,本文重点讲解单行函数.单行函数又可以分为许多类, ...

  3. oracle之单行函数

     单行函数 ①.字符函数 LOWER(x):将x中的每一个单词都转换成小写 UPPER(x):将x中的每一个单词都转换成大写 INITCAP(x): 将x中的每一个单词的首字母转换成大写 CONC ...

  4. Oracle之单行函数(字符串函数/数字函数/转换函数/日期函数/通用函数)

    虚拟表DUAL介绍: dual是一张虚拟表,只有一行一列,用来构成select的语法规则. Oracle的查询中,必须使用"select 列- from 表"的完整语法,当查询单行 ...

  5. Oracle简单的函数语言

    函数:这里的函数相当于java中写好的一些方法,有名字,可以传递参数,实现某一项具体功能. 函数分为: 1.单行函数 1.字符函数 2.日期函数 3.数字函数 4.转换函数 2.分组函数(后面的章节再 ...

  6. ORACLE SQL单行函数(三)【weber出品必属精品】

    16.L:代表本地货币符,这个和区域有关.这个时候我们想来显示一下人民币的符号:¥ $ vi .bash_profile ---写入如下内容: export NLS_LANG='SIMPLIFIED ...

  7. ORACLE SQL单行函数(一)【weber出品必属精品】

    1.SUBSTR:求父串中的子串 SUBSTR('HelloWorld',1,5) 1:代表子串的起始位置,如果为正,正数,如果为负,倒数 5:代表字串的终止位置,只能向右数,可以省略,如果省略就是数 ...

  8. ORACLE SQL单行函数(二)【weber出品必属精品】

    11.dual:虚表,任何用户都可以使用,表结构如下: SQL> desc dual Name Null? Type -------------------------------------- ...

  9. Oracle内置函数之数值型函数

    think different

随机推荐

  1. 【JS】Beginner3 & 4 & 5 & 6:Maths & Logic & Conditonal & Looping

    1.number operator () * / + - 2.logic make decisions in code compare values to produce a boolean valu ...

  2. 【HTML】Intermediate4:Tables:rowspan and colspan

    1.</th> header cell As with td elements,these must be enclosed inside tr elements 2.</tr co ...

  3. 如何从Windows Phone 生成PDF文档

    我需要从我的Windows Phone应用程序生成PDF. 遗憾的是没有标准的免费的PDF生成库在Windows Phone上运行. 我不得不自己生成PDF,通过直接写入到文件格式. 这竟然是真的很容 ...

  4. 转移python

    这段时间一直学python,工作需要做一个基于python的web管理系统,恶补Django. 之前一直觉得开发人员只需要掌握了某个技术就OK了,没有重视总结学习的知识,最近经历的事情让我改变了之前的 ...

  5. 初探WebService

    写博客也是一件非常费时的事儿啊,之前配置服务器和客户端的Oracle数据库搞了很久,搞定之后懒的记录,现在想想如果让我再配一次,估计又要花很长时间了. 所以把做过的东西整理整理记录下来还是很有必要的, ...

  6. POJ1811- Prime Test(Miller–Rabin+Pollard's rho)

    题目大意 给你一个非常大的整数,判断它是不是素数,如果不是则输出它的最小的因子 题解 看了一整天<初等数论及其应用>相关部分,终于把Miller–Rabin和Pollard's rho这两 ...

  7. CentOS搭建VSFTP

    1.先看看有没有安装 rpm -qa | grep vsftpd 如果没有提示,说明没有安装.接下来,我们安装一个ftp. 2.yum安装vsftpd: yum -y install vsftpd 安 ...

  8. JavaScript- The Good Parts Chapter 5 Inheritance

    Divides one thing entire to many objects;Like perspectives, which rightly gazed uponShow nothing but ...

  9. Android Studio快捷键指南(本文持续更新)

    这是我在使用Android Studio过程中接触到的一些快捷键,和大家分享,后面会继续完善此文,也欢迎大家踊跃补充,一起完善. 快捷键 删除并剪贴行:Ctrl+X 复制一行:Ctrl+D 代码格式整 ...

  10. android开发之wheel控件使用详解

    出门在外生不起病呀,随便两盒药60多块钱.好吧,不废话了,今天我们来看看wheel控件的使用,这是GitHub上的一个开源控件,用起来十分方便,我们可以用它做许多事情,比如做一个自定义的datepic ...