1. 程式人生 > >oracle學習筆記單行函數

oracle學習筆記單行函數

oracle 單行函數的用法詳解

單行函數

只對一行進行變換 每行返回一個結果

單行函數分 字符、數值、日期、轉換、通用

字符函數:大小寫控制函數、字符控制函數

大小寫控制函數:lower, upper, initcap

字符控制函數:concat,substr,length,instr,lpad|rpad,trim,replace

lower,upper,initcap

select lower(‘SQL‘) from dual;
--結果 sql
select upper(‘sql‘) from dual;
--結果 SQL
select initcap(‘SQL COurs‘) from dual;
--結果 Sql Cours 首字母大寫

concat,substr,length,instr,lapd|rpd,trim ,replace

select concat(‘hello‘,‘world‘) from dual; //結果 helloworld 
select substr(‘HelloWorld‘,1,4) from dual; //結果 Hell  從第一個字符開始取4個字符 
select length(‘hellowrld‘) from dual; //結果 9 求字符長度*/
select instr(‘Helloword‘,‘w‘) from dual; //結果 6 第一次出現W的位置
select lpad(salary ,10,‘&‘) from employees ; //結果 &&&&&&2600 左填充& 
select rpad(salary ,10,‘&‘) from employees ; //結果 2600&&&&&& 左填充& 
select trim(‘H‘ from ‘HHllWoHldHH‘)from dual; //結果 llWoHld 去首尾不去中間

數字控制 round,trunc,mod

select round(45.36954,4) from dual; //  45.3695四舍五入
select trunc(45.36954,3) from dual; //  45.369 截斷
select mod(1600,300) from dual;     //   100 求余數

日期控制 日期只可加減 months_betwwen,add_months,next_day,last_day

兩個日期相減返回日期之間相差的天數

可以用數字除24來向日期中加上或減去天數

--查詢公司中入職時間是每個月最後兩天的員工
select last_name,to_char(hire_date,‘yyyy-mm-dd‘) hdate 
from employees 
where hire_date=last_day(hire_date)-1
--查詢到2005年入職超過5年的員工
select last_name,to_char(hire_date,‘yyyy-mm-dd‘) from employees
where to_date(‘2005-12-31‘,‘yyyy-mm-dd‘)-hire_date >=5
下個月的今天(系統時間上加1個月)
select add_months(sysdate,1) from dual;
--兩天後的日期
select next_day(sysdate,2) from dual;


oracle學習筆記單行函數