Java笔记oracle--SQL基础篇(一)

时间:2022-05-02 16:00:10

Oracle数据类型
①数值类型:number(p,s)
p:表示数字的总位数
s:表示小数点后有几位 ,s为0 则number表示整数
例:id number(10),salary number(6,2)
:表示salary的最高工资是4位数,小数点后有2位
②关于数值操作—-数值函数
round:四舍五入
eg: select round(23.637,2) from dual;–23.64

select round(23.637,0) from dual; --24
select round(25.637,-1) from dual; --30

trunc:截取(找到位置,直接截断)

select trunc(45.627,2) from dual; --45.62
select trunc(45.627,0)from dual;  --45
select trunc(45.627,-1) from dual; --40

mod:求余数 mod(m,n) 如果n为0,则直接返回m的值

select mod(30,7) from dual;  --2

CEIL :(翻译:地板—所以不能再往下了):向上取整数

select ceil(45.321) from dual;   --46

floor:(翻译:天花板–所以不能再往上了):向下取整数

select floor(45.321) from dual;  --45

<2>字符类型(默认单位是字节)

① char:固定字符长度 格式:char(n)
varchar2:可变字符长度 格式:varchar2(n)

eg: name varchar2(20), gender char(1)
注意:char占用的空间是固定的n个字节
varchar2占用的空间根据保存的数据长度变化

②关于字符串操作

concat(char1,char2)
:连接字符串,只能有两个参数
eg: select concat(ename,salary) from employees;
注:‘||’连接符。 concat 和‘||’的区别是concat只能有两个参数

02.trim/ltrim/rtrim
:截取子字符串
trim(char2 from char1):从char1的前后截去char2
ltrim(char1,char2):从char1的左面截去char2
rtrim(char1,char2):从char1的右面截取char2

 eg`select trim('c' from 'chaoge') from dual;'
   `select ltrim('chaogechao','chao') from dual;`
    select rtrim('chaogechao','chao') from dual;`
 --运行结果分别是: haoge  / gechao  / chaoge

03.upper/lower/initcap
这三个函数是将英文转换大小写的
upper:全部转换为大写
lower:全部转换为小写
initcap:将英文单词首字母转为大写(挺重要)
eg:“`
select upper(‘thing in java’),
lower(‘THINK IN JAVA’),
INITCAP(‘think in java’) from dual;
运行结果:
Java笔记oracle--SQL基础篇(一)

04.substr(char,m,n):
从字符串的m位置开始截取n个字符。计数从1开始
eg:select substr('think in java',7,5) from dual;
运行结果: Java笔记oracle--SQL基础篇(一)

05.instr(char1,char2)
用来返回字符串中子串的位置,没有找到则返回0
eg:select instr('helloworld','llo') from dual;
运行结果: Java笔记oracle--SQL基础篇(一)

06 lpad,rpad
lpad(char1,n,char2):左补位函数
rpad(char1,n,char2):右补位函数
eg:select lpad('java',10,'*'),rpad('java',10,'*') from dual;
运行结果是:Java笔记oracle--SQL基础篇(一)
<3>日期类型
01.date:
eg:`hiredate date 表示入职日期是一个DATE类型
timestamp:时间戳
两者的区别是timestamp不仅可以保存日期和时间,还能保存小数秒,可以精确到纳秒

02.sysdate:
系统时间,默认的显示格式:DD-MON-RR
eg:select sysdate from dual;
Java笔记oracle--SQL基础篇(一)

03.日期常用函数

next_day(date,n):(不是明天!)
表示当前日期date的下一个礼拜几是哪年那月几号?
n:表示周几 ,取值:1-7,代表周日到周六。
eg :select next_day(sysdate,2) from dual;
Java笔记oracle--SQL基础篇(一)

last_day(date):
返回当前日期所在月的最后一天
eg:select last_day(sysdate) from dual;
Java笔记oracle--SQL基础篇(一)

add_months(date,n):
在当前月的基础上再加上n个月
eg:select add_months(sysdate,3)from dual;

months_between(date1,date2)
– 计算两个日期之间的相差的月份

least(date1,date2,date3):
–比较多个日期之间的最小值

–greatest(date1,date2,date3):
比较多个日期之间的最大值

extract:提取日期中的年或月或日
eg:select extract(year from sysdate) from dual;
Java笔记oracle--SQL基础篇(一)
eg:select extract(month from sysdate) from dual;

04.日期转换函数to_char 和to_date

to_date:将字符串按照指定格式转换为日期

select to_date('2012-10-26',' yyyy-mm-dd') from dual;

运行结果:
Java笔记oracle--SQL基础篇(一)

to_char:将日期按照指定格式转换为字符串

select to_char(sysdate,'yy-mm-dd hh24:mi:ss') from dual;

运行结果:
Java笔记oracle--SQL基础篇(一)

<4>空值操作–null

   在数据库中,任何数据类型可以取null,空值是一个特殊的值,比较的时候使用 is null 和 is not null 。但空值不是0!

通用函数:将null转变为非空值,也适用于其它数据类型。

nvl(e1,e2) :如果e1是空值,则取e2的值。

nuv2(e1,e2,e3):如果e1是空值,则取e3的值,否则取e2的值。