CASE는 함수와 성격이 같지만 표현 방식에 있어서 함수라기보다는 구문에 더 가깝다.
문장으로는 '~이면 ~이고, ~이면 ~이다'식으로 표현된다.
필요에 따라 각 CASE를 여러 개로 늘릴 수 있다.
Oracle에서는 DECODE 함수로 표현한다.
다음 구문은 모두 같은 결과값을 반환한다.
-- 1
CASE when subway_line = 1 then 'blue'
when subway_line = 2 then 'green'
when subway_line = 3 then 'orange'
else 'gray'
END as line_color
-- 2
CASE subway_line
when 1 then 'blue'
when 2 then 'green'
when 3 then 'orange'
else 'gray'
END as line_color
-- 3
decode (subway_line, 1, 'bule', 2, 'green', 3, 'orange', 'gray') as line_color
다른 명령어와 합친 구문 코드로 보면
-- 전체 코드
-- CASE
-- 함수라기 보다는 구문에 가까움
-- ~이면 ~이고, ~이면 ~이다
-- 다음 구문은 모두 같은 결과값을 반환한다.
create table subway_info(
subway_line number(2),
start_point varchar2(10),
end_point varchar2(10)
);
insert into subway_info values(1, '인천', '소요산');
insert into subway_info values(2, '순환선', '순환선');
insert into subway_info values(3, '대화', '오금');
insert into subway_info values(4, '오이도', '당고개');
insert into subway_info values(5, '방화', '마천');
select * from subway_info;
--1.
select subway_line,
CASE when subway_line = 1 then 'blue'
when subway_line = 2 then 'green'
when subway_line = 3 then 'orange'
else 'gray'
END as line_color
from subway_info;
--2.
select subway_line,
CASE subway_line
when 1 then 'blue'
when 2 then 'green'
when 3 then 'orange'
else 'gray'
END as line_color
from subway_info;
--3. 오라클
select subway_line,
decode (subway_line, 1, 'bule', 2, 'green', 3, 'orange', 'gray') as line_color
from subway_info;