web developer

[sql] Case When Then Else End 본문

SQL/Oracle SQL

[sql] Case When Then Else End

trueman 2021. 12. 28. 13:39
728x90
728x90

CASE 표현식의 기본 구성은 다음과 같다.

Case when 조건식1 then 결과1
         when 조건식2 then 결과2
         else 결과


* 조건식 사이에 콤마금지
* case 문은 end로 끝남
* ansi sql 형식 지원
* 결과에 null 사용금지


/* 조건식이 1개일때*/
case 
    when a.animal_name= '사자' 
    then '사자임' /*a.animal_name 사자임으로 변경*/
    else '아님' /* 조건이 만족하지 않을시 '아님'으로 변경*/
end
/* 위와 동일*/
case 
    a.animal_name
    when '사자'
    then '사자임'
    else '아님'
end
 
/* 조건식 2개 이상일때*/
case 
    when a.animal_name='사자'
    then '사자임'
    when a.animal_name='하마'
    then '하마임'
    else '아님'
end
/* 위와 동일*/
case 
    a.animal_name
    when '사자'
    then '사자임'
    when '하마'
    then '하마임'
    else '아님'
end

CASE
   WHEN RANK = '사원' [조건식1] THEN 2000000 [조건식1 만족시 출력데이터]
   WHEN RANK = '대리' [조건식2] THEN 2500000 [조건식2 만족시 출력데이터]
   ELSE 3500000[조건에 만족하지 않을 때 출력데이터] 
   END
AS 급여[컬럼명]


출처 1 : https://joke00.tistory.com/103   
출처 2 : https://ddacker.tistory.com/14     
출처 3 : https://chocoball3.tistory.com/152

728x90
728x90

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

[sql] INSTR 함수  (0) 2022.01.07
[sql] SUBSTR 함수  (0) 2022.01.07
[sql] 오라클 CONCAT, ||를 이용한 문자열 합치기  (0) 2021.12.20
[SQLD] 계층형 질의  (0) 2021.11.18
[SQLD] 집합(SET) 연산자  (0) 2021.11.16
Comments