-- 去重: distinct
--利用当前emp表复制一份新表(包含数据):
create table emp2 as select * form emp;
-- 利用当前emp表复制一份新表(只复制表结构):
create teble emp3 as select * from emp where 1=2;
--表别名,拼接操作 'A' || B --> 将A拼接到B的前面:
select 'E' || e.empno eno , e.ename myName from emp e;
--replace() 替换操作:
select replace('hello','1','中') from dual;
--length() 返回长度 :
select length('hellow world') from dual;
--substr() 截取字符串:
select substr('123456') from fual;
--instr() :
select instr('hello world ','w','1','2') from dual; (查找hello world 第2次出现w的数字位置)
--round() 不保留小数
select round(1000.1) from dual; -- 1000
--保留小数round ( 参数 , 保留几位小数 ):
select round(998.982, 2) from dual ; -- 998.98
--截取小数: trunc(n)
--向上取整: cell(n)
--向下取整: floor(n)
--取余: mod(m,n) ==>m对n取余
-- 日期函数
select sysdate from dual;
-- months_between返回两个日期间的月数
select months_between(sysdate,hiredate) from emp;
select hiredate from emp;
-- add_months() 返回所加月数后的最后日期
select add_months(sysdate,3) from dual;
-- next_day返回下一个星期的星期几
select next_day(sysdate,'星期一') from dual;
-- last_day() 返回日期中的最后一天
select last_day(sysdate)from dual;
/*
转换函数
*/
-- to_char
select to_char(sysdate,'YYYY') from dual;
select to_char(e.hiredate,'YYYY-mm-dd hh24:mi:ss dy') from emp e;
-- 数字转换
select to_char(123456,'999999')from dual; -- 123456
select to_char(123456,'99')from dual; -- ###
select to_char(123456,'L999999')from dual; -- ¥123456
select to_char(123456,'$999999')from dual; -- $123456
select to_char(123456,'0999999')from dual; -- 0123456
-- to_date 将字符转换为日期,多用于更新表
update emp e set e.hiredate=to_date('2008-08-12','yyyy-mm-dd')
-- to_number 将数字的字符串类型转换为数字类型
select to_number('09') + to_number('10') from dual; -- 19
-- nvl() nvl2() coalese
Oracle sql常用的查询函数
最新推荐文章于 2024-07-30 19:32:58 发布