1.Oracle 數據庫中的to_date()函數的使用: 往emp表中插入一條記錄:
SQL
>
insert
into emp
values(
1234,
'
LIZELU
',
'
BOSS
',
1234,
'
1980-12-06
',
10000.0,
0,
30
);
insert
into emp
values(
1234,
'
LIZELU
',
'
BOSS
',
1234,
'
1980-12-06
',
10000.0,
0,
30
) ORA
-
01861: 文字與格式字符串不匹配
--
日期格式不對
使用to_date()函數搞定:格式to_date(
'
1965-02-05
',
'
yyyy-mm-dd
');
?
2.Oracle中的字符函數:
字符函數是Oracle中最常用的函數, lower(char); 把字符串轉換為小寫格式; upper(char);把字符串轉換為大寫格式; length(char);返回字符串的長度; substr(char,m,n);取字符串的字串; replace(char,search_char,replace_str);
1.將所有員工的名字按小寫的格式輸出
select
lower(emp.ename)
from emp;
2.顯示正好為5個字符的名字;
select ename
from emp
where length(ename)
=
5;
3.顯示姓名的前三個字符;substr(char,2,3);代表從第二個取,取三個字符;
select substr(ename,
1,
3)
from emp;
4.顯示姓名要求首字母大寫,其余的小寫; 分成三部走: (1)把首字母大寫:
select
upper(substr(emp.ename,
1,
1))
from emp;
(2)把后面的字母小寫:
select
lower(substr(ename,
2,length(ename)
-
1))
from emp;
(3)把兩個字符串連接起來 ||(管道符是連接作用的)
select
upper(substr(emp.ename,
1,
1))
||
lower(substr(ename,
2,length(ename)
-
1))
from emp;
?
?
5.把名字中的A轉換為a;
select
replace(ename,
'
A
',
'
a
')
from emp;
?
3.Oracle 中的數學函數: 1.round(n,[m]):四舍五入,省略m則四舍五入到整數位,m為小數點的位數;
select
round(sal,
1)
from emp
where ename
=
'
MILLER
';
2.trunc(n,[m]):保留小數位,m為小數位的個數
select trunc(sal,
1)
from emp
where ename
=
'
MILLER
';
3.mod(n,m):去小數;
4.floor(n):返回小于等于n的最大整數;? ceil(n):返回大于等于n的最小整數
SQL
>
select
floor(sal)
from emp
where ename
=
'
MILLER
';
--
向下取整
FLOOR
(SAL)
--
--------
1300
SQL
>
select ceil(sal)
from emp
where ename
=
'
MILLER
';
--
向上取整
CEIL(SAL)
--
--------
1301
其他數學函數: abs(n):返回數字n的絕對值。 acos(n),asin(n),stan(n) 返回數字的反余弦,反正弦,反正切的值 exp(n):返回e的n次冪; log(m,n);返回對數值; power(m,n);返回m的n次冪
?
4.Oracle中的日期函數: 日期函數用于處理date類型的數據:默認情況下是dd-mon-yy格式。 (1)sysdate:該函數返回系統時間
SQL
>
select sysdate
from
dual; SYSDATE
--
---------
2014
-
4
-
13
9
(2)add_moths(d,n);
顯示入職8個多月的職工;
select
*
from emp
where sysdate
>add_months(emp.hiredate,
8);
?
(3)last_day(d);返回當前日期該月的最后一天
select last_day(emp.hiredate)
from emp;
?
( 4)顯示員入職的天數
SQL
>
select ename,
round(sysdate
-emp.hiredate) "入職天數"
from emp;
?
(5) 找出個月的倒數第3天入職的員工
SQL
>
select
*
from emp
where (last_day(emp.hiredate)
-emp.hiredate)
=
2;
?
?
?
?
5.Oracle中數據類型的轉換 to_char():把數據轉換為字符串類型:to_char(字符串,類型);
1.日期轉換
SQL
>
select to_char(sysdate,
'
yyyy/mm/dd hh24:mi:ss
')
from
dual; TO_CHAR(SYSDATE,
'
YYYY/MM/DDHH2 ------------------------------ 2014/04/13 10:13:52
2.顯示1980年入職的員工信息
SQL
>
select
*
from emp
where to_char(emp.hiredate,
'
yyyy
')
=
1980
; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
--
--- ---------- --------- ----- ----------- --------- --------- ------
1234 LIZELU BOSS
1234
1980
-
12
-
6
10000.00
0.00
30
7369 SMITH CLERK
7902
1980
-
12
-
17
800.00
20
?
6.Oracle中的系統函數:sys_context(); 1) terminal 當前會話客戶所對應的終端標識符
SQL
>
select sys_context(
'
USERENV
',
'
terminal
')
from
dual; SYS_CONTEXT(
'
USERENV
',
'
TERMINA -------------------------------------------------------------------------------- WEB-A93B1E61669
2) language 語言
SQL
>
select sys_context(
'
USERENV
',
'
language
')
from
dual; SYS_CONTEXT(
'
USERENV
',
'
LANGUAG -------------------------------------------------------------------------------- SIMPLIFIED CHINESE_CHINA.ZHS16GBK
?
3)db_name 當前的數據庫實例名稱
SQL
>
select sys_context(
'
USERENV
',
'
db_name
')
from
dual; SYS_CONTEXT(
'
USERENV
',
'
DB_NAME -------------------------------------------------------------------------------- orcl
?
4)session_user 當前會話所對應的數據庫
SQL
>
select sys_context(
'
USERENV
',
'
session_user
')
from
dual; SYS_CONTEXT(
'
USERENV
',
'
SESSION -------------------------------------------------------------------------------- SCOTT
?
5)current_schema:查看當前方案
SQL
>
select sys_context(
'
USERENV
',
'
current_schema
')
from
dual; SYS_CONTEXT(
'
USERENV
',
'
CURRENT -------------------------------------------------------------------------------- SCOTT
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
