Oracle常見函式

2021-08-03 03:01:27 字數 3029 閱讀 3914

一、字元函式

lower(char):將字串轉化為小寫。

upper(char):將字串轉化為大寫。

length(char):返回字串的長度。注:lengthb(char)返回的是位元組的長度,例如:length(char)=lengthb(char)用於判斷是不是中文。

substr(char, m, n):擷取字串的子串,n代表取n個字元的意思,不是代表取到第n個。注:m等於0或者1都表示從第乙個字元開始。

replace(char, sub_string, replace_string):把char字串中的sub_string替換成replace_string。

instr(source_string,destination_string,start,total):source_string源字元,destination_string:目標字元,start:查詢的開始位置,預設是1,total:查詢第幾次出現destination_string的位置,預設1。例如:select instr('abcabc','c',1,2) from dual;結果等於6。

rpad:在列的右邊貼上字元。例如:select rpad('oracle',10,'*') from dual;

lpad:在列的左邊貼上字元。例如:select lpad(rpad('oracle',10,'*'),15,'*')from dual;

二、數字函式

round(m,[n]):該函式用於執行四捨五入。例如:round(1.256,2),結果:1.26;round(168.256,-2),結果:200。

trunc(n,[m]):該函式用於擷取數字。例如:trunc(1.256,2),結果:1.25;trunc(168.256,-2),結果:100例如:trunc(1.256),結果:1。

select trunc(sysdate)-1 from dual;

2017/6/29

今天為2017/6/30,獲得的是昨天的日期

trunc(date):為指定元素而截去的日期值。例如:select sysdate from dual;-----------2016/8/4 21:51:14 系統時間

select trunc(sysdate,'yyyy') from dual;------2016/1/1  返回當年第一天

select trunc(sysdate,'dd') from dual;------2016/8/4      返回當月第一天

select trunc(sysdate,'d') from dual;------2016/7/31     返回當前星期的第一天

mod(m,n):取餘函式。例如:mod(10,2),結果:0,mod(10,3),結果:1。

floor(n) :返回小於或是等於n的最大整數

ceil(n) :返回大於或是等於n的最小整數。例如:select floor(-1.25) from dual;

ceil 兩個時間相減預設的是天數;

ceil 兩個時間相減預設的是天數*24*60 為相差的分鐘數

----10分鐘以內的資料

case

when a.dev_status = 0

and ceil(( sysdate - dev_operate_time ) * 24 * 60 ) <= 10 then

when a.dev_status = 8 then

'遮蔽' else '--'

end zt,

abs(n) :返回數字n的絕對值。例如:select abs(-1.25) from dual;

三、日期函式

sysdate:返回系統時間。例如:select sysdate from dual;

add_months(time,months):函式可以得到某一時間之前或之後n個月的時間。例如:select add_months(sysdate,-6) from dual; --該查詢的結果是當前時間半年前的時間。

四、轉換函式

to_char()函式。例如:select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;

yy:兩位數字的年份 2004-->04

yyyy:四位數字的年份 2004

mm:兩位數字的月份 8 月-->08

dd:兩位數字的天 30 號-->30

hh24: 8點-->20

hh12:8點-->08

mi、ss-->顯示分鐘\秒

to_date():用於將字串轉換成date型別的資料。例如:select to_date('2016-08-04','yyyy-mm-dd') from dual;

select sysdate from dual;

select to_char(sysdate,'yyyy-mm-dd') from dual;

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

select case

when to_char(sysdate,'yyyy-mm-dd')='2020-04-01' then

1 else

0end 別名

from dual;

select case

when to_date('2020-04-01 16:57:10', 'yyyy-mm-dd hh24:mi:ss')=sysdate then

1 else

0end 別名

from dual;

四、其它函式

nvl():空值轉換。例如:select nvl(null,'為空') from dual;

decode():它將輸入數值與函式中的引數列表相比較。例如:select decode('n','x','女','y','男','不確定') from dual;

wm_concat(column):將多條記錄的column欄位組成乙個用逗號隔開的字串。例如:select wm_concat(ename) from emp;

oracle 常見函式

upper select upper abcde from dual select from emp where ename upper smith lower select lower abcde from dual initcap 首字母大寫 其他字母小寫 select initcap enam...

Oracle 常見函式用法

1.decode函式 select id,name,decode lever,1,教授 2,副教授 3,導師 講師 position,select classnum from class t where t.num s.num num from teacher s where t.lever in ...

oracle 常見日期函式

日期函式 add months d,n 日期d加n個月 last day d 包含d的月份的最後一天的日期 month between d,e 日期d與e之間的月份數,e先於d new time d,a,b a時區的日期和時間d在b時區的日期和時間 next day d,day 比日期d晚,由day...