字符函數(shù): Lower(char):將字符串裝換為小寫的格式 Upper(char):將字符串裝換為大寫的格式 length(char):返回字符串的長度 substr(char,m,n):從m開始截取n個字符串 1)查詢emp表,,名字用小寫顯示: select lower(ename) from emp ; 2)查詢名字的長度是5個字符的員工: select * from emp where length(ename)=5; 3)select substr(ename,2,3) from emp; 這樣,SMITH就變成了MIT 4)使名字的第一個字符大寫,,其它字符小寫: select upper(substr(ename,1,1))||lower(substr(ename,2,length(ename)-1)) from emp; ||:這個符號在oracle中是個連接符,,就像Java中用“+”連接兩個String類型的字串一樣。 5)select replace(ename,'A','a') from emp; 把ename字段中的所有‘A’替換成‘a’,,當(dāng)然,,這樣替換之后僅僅是對查詢結(jié)果的顯示起了作用,不會真的在數(shù)據(jù)庫中作替換的 6)to_date函數(shù) insert into myemp values(7903,'MORFLAME','ANALYST',7566,to_date('1988-1-1','yyyy-mm-dd'),2345.23,555.55,20); 本來oracle的默認(rèn)日期格式是‘01-1月-1988’,,如果不習(xí)慣,,那么在做插入的時候就可以使用這個函數(shù)來用我們喜歡的日期格式做插入,比如to_date(‘1988/1/1’,’yyyy/mm/dd’),,這也是可以的,。 7)round函數(shù):四舍五入求值 SQL> select round(sal),sal from myemp where ename= 2 'MORFLAME'; ROUND(SAL) SAL ---------- --------- 2345 2345.23 還可以控制小數(shù)的位數(shù): select round(sal,1),sal from myemp where ename='MORFLAME'; ROUND(SAL,1) SAL ------------ --------- 2345.2 2345.23 8)trunc函數(shù):不四舍五入,直接舍掉 SQL> select trunc(comm,1),comm from myemp where ename='MORFLAME'; TRUNC(COMM,1) COMM ------------- --------- 555.5 555.55 第二個參數(shù)是-1的情況 SQL> select round(comm,-1),comm from myemp where ename='MORFLAME'; ROUND(COMM,-1) COMM -------------- --------- 560 555.55 9)ceil函數(shù):求比某個數(shù)大的最小整數(shù) SQL> select ceil(comm),comm from myemp where ename='MORFLAME'; CEIL(COMM) COMM ---------- --------- 556 555.55 10)floor函數(shù):求比某個數(shù)小的最大整數(shù) SQL> select floor(comm),comm from myemp where ename='MORFLAME'; FLOOR(COMM) COMM ----------- --------- 555 555.55 11)取模函數(shù)mod SQL> select mod(12,3) from dual;--此處的dual表是個虛擬表,,僅僅用來測試函數(shù)而已。 MOD(12,3) ---------- 0 12)abs,,求絕對值函數(shù) SQL> select abs(-3) from dual; ABS(-3) ---------- 3 13)其它數(shù)學(xué)函數(shù)還有:cos,cosh,exp,ln,log,sin,sinh,sqrt,tan,tanh,acos,asin,atan…… 14)日期函數(shù) sysdate:返回系統(tǒng)時間 select sysdate from dual; add_months(d,n):從d這個日期開始加上n個月的日期 求8個月前入職的員工: select * from myemp where sysdate>add_months(hiredate,8); 求入職天數(shù): select sysdate-hiredate "入職天數(shù)",ename from myemp; 入職天數(shù) ENAME ---------- ---------- 10644.4805 SMITH 10579.4805 ALLEN 8073.48059 MORFLAME last_day(d):返回指定日期所在月份的最后一天 求在本月的倒數(shù)第三天入職的員工: select ename,hiredate from emp where last_day(hiredate)-2=hiredate; to_char函數(shù): SQL> SELECT ename,to_char(hiredate,'yyyy-mm-dd hh24:mi:ss') from emp; ENAME TO_CHAR(HIREDATE,'YYYY-MM-DDHH ---------- ------------------------------ SMITH 1980-12-17 00:00:00 ALLEN 1981-02-20 00:00:00 WARD 1981-02-22 00:00:00 SQL> SELECT ename,to_char(hiredate,'yyyy-mm-dd hh24:mi:ss'),to_char(sal,'L99,999.99') from emp; ENAME TO_CHAR(HIREDATE,'YYYY-MM-DDHH TO_CHAR(SAL,'L99,999.99') ---------- ------------------------------ ------------------------- SMITH 1980-12-17 00:00:00 ¥800.00 ALLEN 1981-02-20 00:00:00 ¥1,600.00 WARD 1981-02-22 00:00:00 ¥1,250.00 JONES 1981-04-02 00:00:00 ¥2,975.00 yy:兩位數(shù)字的年份:2004年->04 yyyy:四位數(shù)字的年份 mm:兩位數(shù)字的月份:8月->08 dd:兩位數(shù)字的天:30號->30 hh24:24小時制 hh12:12小時制 mi:顯示分鐘 ss:顯示秒 9:顯示數(shù)字,,并忽略前面0 0:顯示數(shù)字,,如位數(shù)不足,則用0補(bǔ)齊 .:在指定位置顯示小數(shù)點 ,,:在指定位置顯示逗號 $:在數(shù)字前加美元符號 L:在數(shù)字前加本地貨幣符號 C:在數(shù)字前加國際貨幣符號 G:在指定位置顯示組分隔符 D:在指定位置顯示小數(shù)點符號(.) 如:1980年入職的員工: SQL> SELECT ename,hiredate,to_char(sal,'L99,999.99') from emp where to_char(hiredate,'yyyy')='1980'; ENAME HIREDATE TO_CHAR(SAL,'L99,999.99') ---------- ----------- ------------------------- SMITH 1980/12/17 ¥800.00 sys_context函數(shù):系統(tǒng)函數(shù) terminal:當(dāng)前會話客戶多對應(yīng)的終端的標(biāo)識符 language:語言 db_name:數(shù)據(jù)庫名稱 nls_date_format:當(dāng)前會話客戶所對應(yīng)的日期格式 session_user:當(dāng)前會話客戶多對應(yīng)的數(shù)據(jù)庫用戶名 current_schema:當(dāng)前會話客戶所對應(yīng)的默認(rèn)方案名 host:返回數(shù)據(jù)庫所在主機(jī)的名稱 SQL> select sys_context('USERENV','db_name') from dual; SYS_CONTEXT('USERENV','DB_NAME -------------------------------------------------------------------------------- orcl SQL> select sys_context('USERENV','terminal') from dual; SYS_CONTEXT('USERENV','TERMINA -------------------------------------------------------------------------------- BLUENIGHT-PC SQL> select sys_context('USERENV','language') from dual; SYS_CONTEXT('USERENV','LANGUAG -------------------------------------------------------------------------------- SIMPLIFIED CHINESE_CHINA.ZHS16GBK SQL> select sys_context('USERENV','nls_date_format') from dual; SYS_CONTEXT('USERENV','NLS_DAT -------------------------------------------------------------------------------- DD-MON-RR SQL> select sys_context('USERENV','session_user') from dual; SYS_CONTEXT('USERENV','SESSION -------------------------------------------------------------------------------- SCOTT SQL> select sys_context('USERENV','current_schema') from dual; SYS_CONTEXT('USERENV','CURRENT -------------------------------------------------------------------------------- SCOTT SQL> select sys_context('USERENV','host') from dual; SYS_CONTEXT('USERENV','HOST') -------------------------------------------------------------------------------- WORKGROUP\BLUENIGHT-PC 補(bǔ)充: 取得某個字符的ASCII碼: select ascii(‘A’) from dual;這是取得字符A的ASCII碼值 |
|