select last_name, first_name, nullif(substr(last_name, -1, 1), substr(first_name, -1, 1)) 

  from employees;




'Oracle > SQL Fundamentals I' 카테고리의 다른 글

7일차 # 3-54: Conditional Expressions  (0) 2012.04.12
7일차 # 3-52: COALESCE  (0) 2012.04.12
7일차 # 3-48: NVL, NVL2  (0) 2012.04.12
7일차 # 3-43: RR Date Format  (0) 2012.04.12
7일차 문제(select문)~~  (0) 2012.04.12

★중요 null함수

NVL null값을 바꾸는 함수

NVL2 (식별자,null아닐때,null일때) null값으로 if문을 만드는것

NULLIF 두 개 값을 비교하여 NULL값 또는 사용되어진 두 개값 중 하나를 리턴해 주는 함수 -> NULLIF(a,b)같은값은 null

coalesce 인수중에서 NULL이 아닌 첫 번째 인수를 반환해 주는 함수

 ----------------------------
Conditional Expressions
where 절에 쓸수있다는게 중요 ! 검색을 더잘할수있슴
조건문, if로직

CASE문
SELECT last_name, job_id, salary,
CASE job_id WHEN 'IT_PROG' THEN 1.10*salary
WHEN 'ST_CLERK' THEN 1.15*salary
WHEN 'SA_REP' THEN 1.20*salary
ELSE salary END "REVISED_SALARY"
FROM employees; 

DECODE문
SELECT last_name, job_id, salary,
DECODE(job_id, 'IT_PROG', 1.10*salary,
'ST_CLERK', 1.15*salary,
'SA_REP', 1.20*salary,
salary)
REVISED_SALARY
FROM employees;






select empno, ename, comm, nvl(comm, 0)

  , nvl2(comm, comm, 0)

  from emp;


  select empno, ename, sal, comm

, sal*12 + nvl(comm, 0) as ann_sal

, decode(nvl(to_char(comm), 'null'), 'null', 'sal*12', 'sal*12+comm') as gubun

  from emp;


  select empno, ename, sal, comm, nvl2(comm, sal*12+comm, sal*12) as ann_sal, nvl2(comm, 'sal*12+comm', 'sal*12') as gubun

  from emp;


  select empno, ename, sal, comm

, sal*12 + nvl2(comm, comm, 0) as ann_sal, nvl2(comm, 'sal*12+comm', 'sal*12') as gubun

  from emp;



  * NVL2를 이용해서 NVL의 기능을 구현

select nvl(comm, 0)

    , nvl2(comm, comm, 0) 

from emp;


  * return type

    - NVL: expr1

    - NVL2: expr2

'Oracle > SQL Fundamentals I' 카테고리의 다른 글

7일차 # 3-52: COALESCE  (0) 2012.04.12
7일차 # 3-51: NULLIF  (0) 2012.04.12
7일차 # 3-43: RR Date Format  (0) 2012.04.12
7일차 문제(select문)~~  (0) 2012.04.12
6일차 # 3-42: fx의 효과  (0) 2012.04.10

  - YY Date Format Element

  - RR Date Format Element






alter session set nls_language = american;


drop table df_test1 purge;

create table df_test1 (

 cen varchar2(5),

 naljja date);


alter session set nls_date_format = 'YYYY-MM-DD';

insert into df_test1 values ('20xx', '2012-01-09');

insert into df_test1 values ('00xx', '12-01-09');

insert into df_test1 values ('000x', '2-01-09');

insert into df_test1 values ('20xx', '01-09'); --> ORA-01840: 입력된 값의 길이가 날짜 형식에 비해 부족합니다

commit;


select * from df_test1;


insert into df_test1 values ('19xx', '1912-01-09');

insert into df_test1 values ('20xx', '2052-01-09');

insert into df_test1 values ('19xx', '1952-01-09');

commit;


select * from df_test1;


alter session set nls_date_format = 'YY-MM-DD';

select * from df_test1;


select * from df_test1

where naljja = '12-01-09';


select * from df_test1

where naljja = '52-01-09';



  - 1912년을 찾고 싶다.


select * from df_test1

where naljja = to_date('1912-01-09', 'YYYY-MM-DD');


    또는


select * from df_test1

where naljja = '1912-01-09';


    만약 애플리케이션에서 넘어온 날짜 값이 다음과 같다면: '09/01/1912'


select * from df_test1

where naljja = '09/01/1912';


↓ ↓ ↓ 


select * from df_test1

where naljja = to_date('09/01/1912', 'DD/MM/YYYY');




  - RR Format으로 변환


alter session set nls_date_format = 'RR-MM-DD';

select * from df_test1;


select * from df_test1

where naljja = '12-01-09';


select * from df_test1

where naljja = '52-01-09';


SQL> select * from df_test1

    where naljja = '2052-01-09';

==> 세기 정보를 함께 주면 RR date format이더라도 2052년을 찾을 수 있습니다.



----------------------------


drop table df_test2 purge;

create table df_test2 (

 d_format varchar2(5),

 naljja date);


alter session set nls_date_format = 'YY-MON-DD';


insert into df_test2 values ('YY', '12-JAN-09');

insert into df_test2 values ('YY', '77-SEP-09');


alter session set nls_date_format = 'RR-MON-DD';


insert into df_test2 values ('RR', '12-JAN-09');

insert into df_test2 values ('RR', '77-SEP-09');


commit;


select naljja from df_test2;


alter session set nls_date_format = 'RRRR-MM-DD'; --> RRRR은 YYYY와 똑같습니다.

select * from df_test2;




※강사님이 칠판에 몇번이나 판서를 하면서 강의를 한 부분....결국에는 모르면 YYYY를 써라 이거였음....

'Oracle > SQL Fundamentals I' 카테고리의 다른 글

7일차 # 3-51: NULLIF  (0) 2012.04.12
7일차 # 3-48: NVL, NVL2  (0) 2012.04.12
7일차 문제(select문)~~  (0) 2012.04.12
6일차 # 3-42: fx의 효과  (0) 2012.04.10
6일차 # 3-38: TO_CHAR with Numbers  (0) 2012.04.10

문제 1) 10번 및 20번 부서의 사원들의 정보를 아래와 같이 출력하세요. 

(단, 명시적 변환을 사용하세요)



사원번호 및 이름          급여            입사일

------------------------- --------------- -------------------------

사번:7369 (SMITH)         800달러         1980년 12월 17일

사번:7566 (JONES)         2975달러        1981년 04월 02일

사번:7782 (CLARK)         2450달러        1981년 06월 09일

사번:7788 (SCOTT)         3000달러        1982년 12월 09일

사번:7839 (KING)          5000달러        1981년 11월 17일

사번:7876 (ADAMS)         1100달러        1983년 01월 12일

사번:7902 (FORD)          3000달러        1981년 12월 03일

사번:7934 (MILLER)        1300달러        1982년 01월 23일




해답)



select *

from emp

where deptno in (10, 20);


↓ ↓ ↓


column "사원번호 및 이름" format a25

col 급여 for a15

col 입사일 for a25




문제2) 위 결과에서 급여를 우측정렬하여 출력하세요


해답)



select '사번:'||to_char(empno)||' ('||ename||')' as "사원번호 및 이름"

, LPAD(to_char(sal)||'달러', 10) as 급여

, to_char(hiredate, 'YYYY"년 "MM"월 "DD"일"') as 입사일

from emp

where deptno in (10, 20);



'Oracle > SQL Fundamentals I' 카테고리의 다른 글

7일차 # 3-48: NVL, NVL2  (0) 2012.04.12
7일차 # 3-43: RR Date Format  (0) 2012.04.12
6일차 # 3-42: fx의 효과  (0) 2012.04.10
6일차 # 3-38: TO_CHAR with Numbers  (0) 2012.04.10
6일차 # 3-37: fm의 효과  (0) 2012.04.10

SQL> select last_name, hire_date

    from employees

    where hire_date = to_date('June  7, 2002', 'fxMonth DD, YYYY');


==> ERROR : ORA-01858: a non-numeric character was found where a numeric was expected


SQL> l

 1    select last_name, hire_date

 2    from employees

 3*   where hire_date = to_date('June  7, 2002', 'fxMonth DD, YYYY')

SQL> c/June  7/June 7

 3*   where hire_date = to_date('June 7, 2002', 'fxMonth DD, YYYY')

SQL> /

 where hire_date = to_date('June 7, 2002', 'fxMonth DD, YYYY')

==> ERROR : ORA-01862: the numeric value does not match the length of the format item


SQL> l

 1    select last_name, hire_date

 2    from employees

 3*   where hire_date = to_date('June 7, 2002', 'fxMonth DD, YYYY')

SQL> c/7/07

 3*   where hire_date = to_date('June 07, 2002', 'fxMonth DD, YYYY')

SQL> /


↓ ↓ ↓ 


  * 결론: fx를 사용하면 format element 간의 공백과 자릿수를 맞춰주어야 합니다.

                    (June  7 => June 7, 7 => 07)


※으아...집에서 하니까 안되네...강사님도 여러번 하셨는데... 아무튼 자릿수를 잘 맞춰라~~


oj_SQL.txt


'Oracle > SQL Fundamentals I' 카테고리의 다른 글

7일차 # 3-43: RR Date Format  (0) 2012.04.12
7일차 문제(select문)~~  (0) 2012.04.12
6일차 # 3-38: TO_CHAR with Numbers  (0) 2012.04.10
6일차 # 3-37: fm의 효과  (0) 2012.04.10
6일차 # 3-32: TO_CHAR with Dates  (0) 2012.04.10



'Oracle > SQL Fundamentals I' 카테고리의 다른 글

7일차 문제(select문)~~  (0) 2012.04.12
6일차 # 3-42: fx의 효과  (0) 2012.04.10
6일차 # 3-37: fm의 효과  (0) 2012.04.10
6일차 # 3-32: TO_CHAR with Dates  (0) 2012.04.10
6일차 # 3-29: Explicit conversion  (0) 2012.04.10





'Oracle > SQL Fundamentals I' 카테고리의 다른 글

6일차 # 3-42: fx의 효과  (0) 2012.04.10
6일차 # 3-38: TO_CHAR with Numbers  (0) 2012.04.10
6일차 # 3-32: TO_CHAR with Dates  (0) 2012.04.10
6일차 # 3-29: Explicit conversion  (0) 2012.04.10
6일차 * 자동 변환과 인덱스  (0) 2012.04.10

* format model : http://goo.gl/9fKed

    : a character literal that describes the format of datetime or numeric data stored in a character string

  

  * format elements


  * format model modifiers: toggle

    - fm (fill mode): no leading or trailing blanks in format elements

          <= TO_CHAR

    - fx (format exact): exact matching between the character data and the format model

          <= TO_DATE



  



       -----------------


  






       -----------------














  set autot off

  set timing off





  







  



'Oracle > SQL Fundamentals I' 카테고리의 다른 글

6일차 * 자동 변환과 인덱스  (0) 2012.04.10
6일차 * 3-28: For expression evaluation  (0) 2012.04.10
6일차 Conversion  (0) 2012.04.10
5일차 # 3-22: Date Functions  (0) 2012.04.09
5일차 # 3-20: Date 산술연산  (0) 2012.04.09


SQL> alter session set nls_language = american;

   SQL> select sysdate, hiredate, months_between(sysdate, hiredate), add_months(hiredate, 6)

from emp;


   SQL> select next_day(sysdate, 'Sat'), next_day(sysdate, 'wed')

from dual;

  

   SQL> select last_day('2011-02-11'), last_day('2016-02-21')

from dual;


   SQL> select  sysdate, next_day(add_months(sysdate, 6), 'fri')

from dual;



  * Single-Row functions

   SQL> select sysdate, hiredate, sysdate - hiredate, ceil(sysdate - hiredate)

from emp;



  * ROUND, TRUNC

   SQL> select sysdate, round(sysdate, 'month'), round(sysdate, 'year') from dual;

   SQL> select to_date('2011-12-15'), round(to_date('2011-12-15'), 'month') from dual;


   SQL> select hiredate, trunc(hiredate, 'Month'), trunc(hiredate, 'Year') from emp;

   SQL> select empno, ename, hiredate from emp where trunc(hiredate, 'Month') = '1981-02-01';   --> 1981년 2월 입사자


↑ ↑ ↑


cf.) SQL> select empno, ename, hiredate

         from emp

 where hiredate between to_date('1981-02-01') and to_date('1981-02-28');


   SQL> select empno, ename, hiredate from emp where trunc(hiredate, 'Year')  = '1982-01-01';   --> 1982년 입사자



'Oracle > SQL Fundamentals I' 카테고리의 다른 글

6일차 # Implicit conversion * 3-27 :For assignment  (0) 2012.04.10
6일차 Conversion  (0) 2012.04.10
5일차 # 3-20: Date 산술연산  (0) 2012.04.09
5일차 # 3-17: Working with Dates  (0) 2012.04.09
5일차 Date #3-19: DUAL table  (0) 2012.04.09

 - 시, 분, 초


SQL> alter session set nls_date_format = 'YYYY-MM-DD HH24:MI:SS';

SQL> select sysdate, sysdate + 1, sysdate + 9 + 15/24 + 39/(24*60) + 22/(24*60*60)

    from dual;



  - 오늘 만나서 백일, 천일, 만 후


   SQL> select sysdate, sysdate + 100, sysdate + 1000, sysdate + 10000

from dual;



  - 만난 지 몇 일?


   SQL> select ceil(sysdate - to_date('2001-04-13')) as "만난지 몇 일" from dual;



-------------------------


SQL> alter session set nls_language = korean;


   SQL> select sysdate + (sysdate + 1)

from dual;

==> 오류: ORA-00975: 날짜와 날짜의 가산은 할 수 없습니다


   SQL> select sysdate * 5                        

from dual;

==> 오류: ORA-00932: 일관성 없는 데이터 유형: NUMBER이(가) 필요하지만 DATE임

'Oracle > SQL Fundamentals I' 카테고리의 다른 글

6일차 Conversion  (0) 2012.04.10
5일차 # 3-22: Date Functions  (0) 2012.04.09
5일차 # 3-17: Working with Dates  (0) 2012.04.09
5일차 Date #3-19: DUAL table  (0) 2012.04.09
5일차 Number// # 3-13: ROUND, TRUNC, CEIL, FLOOR, MOD  (0) 2012.04.09

+ Recent posts