**날짜와 관련한 함수들
to_date(문자열, 형식지정문자)
문자열을 Date으로 반환하는 함수
to_char(날짜, 형식지정문자)
날짜를 문자열로 반환하는 함수
select sysdate from dual;
select to_char(sysdate, 'yyyy') from dual;
select to_char(sysdate, 'yyyy/mm') from dual;
select to_char(sysdate, 'mm') from dual;
select to_char(sysdate, 'yyyy/mm/dd hh24:mi:ss') from dual;
months_between(날짜1, 날짜2)
==> 두날짜 사이의 개월수를 반환하는 함수입니다.
날짜1이 더 최근의 날짜여야 합니다.
select months_between(sysdate,'1995/07/29') from dual;
** 날짜 사이의 연산이 가능합니다.
select sysdate-1, sysdate, sysdate+1 from dual;
select to_date('1995/07/29', 'yyyy/mm/dd') + 100 from dual;
** null에 대한 처리
null은 아직 값이 정해 지지 않은 상태를 말하여
0, '', ' '과는 차이가 있어요.
0,'null','',' ' 들은 값이 있는 상태이고
null은 값이 없는 상태입니다.
where phone is null 이라고 검색하였는데
나와야 하는 데이터가 안나온다면
값에 'null'이 들어갔는지 확인할 필요가 있어요.
** null인 레코드를 찾기 위해서는 비교연산자를 사용할 수 없어요
select * from customer where phone = null; ----------> (X)
==> null인 레코드를 찾기 위해서는
is null
is not null
연산자를 이용합니다.
SQL> insert into customer values(5, '박세리', '대한민국 대전', null);
SQL> insert into customer values(14, '안철수', '대한민국 서울', '');
SQL> insert into customer values(15, '심상정', '대한민국 서울', 'null');
select * from customer where phone is null;
CUSTID NAME ADDRESS PHONE
------ ---------- -------------------- ---------------
5 박세리 대한민국 대전
14 안철수 대한민국 서울
** null의 연산결과는 null입니다.
select eno,ename,salary+10 from emp where ename = '윤석열';
SQL> select eno,ename,salary+10 from emp where ename = '윤석열';
ENO ENAME SALARY+10
----- ---------- ----------
1014 윤석열
==> 만약 null인경우 다른 값으로 대체하려면
nvl함수를 이용합니다.
select eno,ename,nvl(salary,0)+10 from emp where ename = '윤석열';
select eno,ename,nvl(salary,(select avg(salary) from emp e2 where e1.dno = e2.dno))+10
from emp e1 where ename = '윤석열';
** 집계함수 계산에 null이 포함된 행은 집계에서 빠진다.
SQL> select sum(salary) from emp;
SUM(SALARY)
-----------
8260
select avg(salary) from emp;
select sum(salary)/count(*) from emp;
select sum(salary)/count(salary) from emp;
SQL> select avg(salary) from emp;
AVG(SALARY)
-----------
590
SQL> select sum(salary)/count(*) from emp;
SUM(SALARY)/COUNT(*)
--------------------
550.666667
SQL> select sum(salary)/count(salary) from emp;
SUM(SALARY)/COUNT(SALARY)
-------------------------
590
==> avg함수는
전체합을 전체레코드의 수로 나누는 것이 아니라
null이 아닌 개수로 나누어 줍니다.
select avg(nvl(salary,0)) from emp;
AVG(NVL(SALARY,0))
------------------
550.666667
==> 전체레코드의 수로 나누기 하려면
nvl함수를 이용하여 다른값으로 대체합니다.
** 해당하는 행이 하나도 없을 경우
sum, avg함수의 결과는 null이 되며
count함수의 결과는 0이 됩니다.
select sum(saleprice), avg(saleprice), count(custid) from orders where
custid = 1;
select sum(saleprice), avg(saleprice), count(custid) from orders where
custid = 14;
SQL> select sum(saleprice), avg(saleprice), count(custid) from orders where
2 custid = 14;
SUM(SALEPRICE) AVG(SALEPRICE) COUNT(CUSTID)
-------------- -------------- -------------
0
SQL> select nvl(sum(saleprice),0), nvl(avg(saleprice),0), count(custid) from orders where
custid = 14;
NVL(SUM(SALEPRICE),0) NVL(AVG(SALEPRICE),0) COUNT(CUSTID)
--------------------- --------------------- -------------
0 0 0
'프로그래밍 공부 정리 > database' 카테고리의 다른 글
시퀀스(sequence), 다중행 연산자 (0) | 2021.11.26 |
---|---|
rownum, 서브쿼리 (0) | 2021.11.25 |
숫자 관련 오라클 함수 (0) | 2021.11.23 |
DCL, DDL, DML (0) | 2021.11.22 |
제약(primary key, not null, unique,default,check) (0) | 2021.11.21 |