oracle函式總結

2021-08-20 09:03:59 字數 3722 閱讀 7323

select initcap('www.baidu.com') from dual -- initcap(a) 單詞首字母大寫函式

select concat('hello','你好') from dual

select 'hello' || '你好' ||  '世界' from dual

select substr('hello你好',5,3) from dual;

--5表示從第幾個字元開始算,第乙個字元為1,中英文統一處理

--3表示連續取幾個字元

select substr('abcde',3,2) from dual --針對字元 從1開始 不區分中英文

select length('hello你好') from dual; -- length(a) 返回a字串字元的個數 

select length('hello趙軍') from dual

select lengthb('hello你好') from dual  --lengthb(a) 返回a字串位元組的個數(乙個漢字三個位元組)

select instr('helloworld','ll') from dual  --從左向右找第一次出現的位置,從1開始

--注意:找不到返回0  大小寫敏感 

--作用 用來判斷某個字元或字串在另乙個字串是否存在 

select lpad('hello',10,'#') from dual; --lpad(a,10,'#') 在a字串左邊追加多個 # 

select lpad('6640',11,'*') from dual  --應用: 隱藏手機號前多少位,隱藏密碼

select rpad('hello',10,'#') from dual;

select trim(' ' from '  he  ll                ') from dual;  -- 去掉字串兩邊空格

select trim('    hel   l  o ') from dual

select trim (0 from 000987002348900)    from dual; --去掉0 在 000***00 左右兩邊的字元 且自能擷取乙個字元

select trim('a' from 'acworaaldaaac') from dual

select replace('hello','l','l') from dual;

select replace('-9998','-9998','-') from dual  --需求  虧損金額用 '-' 在前台展示

select round(3.1415,3) from dual;  -- 四捨五入

select round(228.23458,4) from dual

select trunc(3.1415,3) from dual;  --擷取字串

select mod(10,3) from dual;    --取餘數(取摩)

select mod(23,5) from dual

select sysdate from dual  --2017-04-23 12:36:27

select round(sysdate,'month') from dual;

select round(sysdate,'year') from dual;

select trunc(sysdate,'month') from dual;

select trunc(sysdate,'year') from dual;

select sysdate-1 "昨天",sysdate "今天",sysdate+1 "明天" from dual;

select months_between('31-12月-17',sysdate) from dual;

select add_months(sysdate,1) from dual;

select add_months(sysdate,-1) from dual;

select next_day(sysdate,'星期三') from dual;

select next_day(next_day(sysdate,'星期三'),'星期三') from dual;

select next_day(next_day(sysdate,'星期三'),'星期日') from dual;

select last_day(sysdate) from dual;

select last_day(sysdate)-1 from dual;

select last_day(add_months(sysdate,1)) from dual;

select last_day(add_months(sysdate,-1)) from dual;

--1)日期-日期=天數

--2)日期+-天數=日期

select to_char(sysdate,'yyyy" 年 "mm" 月 "dd" 日 "day') from dual;

select to_char(sysdate,'yyyy"年"mm"月"dd"日" day') from dual

select to_char(sysdate,'yyyy-mm-dd"今天是"day hh24:mi:ss') from dual;

select to_char(sysdate,'yyyy-mm-dd"今天是"day hh12:mi:ss am') from dual;

select to_char(1234,'$9,999') from dual;

select to_char(55555,'99,999') from dual

select to_char(1234,'l9,999') from dual;

select * from emp where hiredate = to_date('2023年12月17日','yyyy"年"mm"月"dd"日"');

或select * from emp where hiredate = to_date('1980#12#17','yyyy"#"mm"#"dd');

或select * from emp where hiredate = to_date('1980-12-17','yyyy-mm-dd');

select to_number('123') from dual;  --字串 轉換成 數值型

/*   case 字段 

when 條件 then 表示式1

when 條件 then 表示式2

else 表示式n

end*/  

-- decode()函式

-- decode(字段,條件1,表示式1,條件2,表示式2,...表示式n)

-- 方式一:

select ename "姓名",job "職位",sal "漲前工資",

case job

when 'analyst' then sal+1000

when 'manager' then sal+800

else sal+400

end "漲後工資"

from emp;

-- 方式二:

select ename "姓名",job "職位",sal "漲前工資",

decode(job,'analyst',sal+1000,'manager',sal+800,sal+400) "漲後工資"

from emp;

Oracle函式總結

instr 源字串,目標字串,起始位置,匹配序號 在oracle plsql中,instr函式返回要擷取的字串在源字串中的位置。只檢索一次,就是說從字元的開始 到字元的結尾就結束。語法如下 引數分析 string1 源字串,要在此字串中查詢。string2 要在string1中查詢的字串.start...

Oracle函式總結

clob字段值替換 replace x,y,z 返回值為將串x中的y串用z串替換後的結果字串。若省略z引數,則將串x中為y串的地方刪除 update ypgcxx.t article info st set st.f content replace st.f content,portal ypgcx...

oracle 函式總結

a.既可以是數字也可以是日期 b.注意一定是從小到大 否則無資料 a.注意轉義字元的書寫 如 select from tpl goods where t.goodsname like 頭孢 escape a.注意null,永遠是最大 nulls last 如 select goodsid,goods...