서버(Server)/DB
DB : Case, where
3DMP
2023. 1. 31. 10:00
use BaseballData;
select birthMonth
from players;
select *,
case birthMonth
when 1 then N'겨울'
when 2 then N'봄'
when 3 then N'가을'
when 8 then N'8이다'
else N'그밖에'
end as birthSeason
from players;
switch case 와 비슷한걸 알 수 있다
위에서 end as birthSeason 끝에 새로 추가된 컬럼의 이름을 birthSeason 으로 지정하겠다는 얘기다
![]() |
![]() |
case 의 where 조건에 맞춰 문자로 변환되어 추가 된것을 볼 수 있다
아래 처럼 조건문을 추가 하는 구문 또한 있다
select *,
case
when birthMonth <=1 then N'back'
when birthMonth <=3 then N'나이스'
when birthMonth <=6 then N'앜'
when birthMonth <=9 then N'9이하'
when birthMonth <=12 then N'12이하'
else N'그밖에'
end as birthSeason
from players;
위 구문들에서 else 구문이 없다면 else 에 에 해당 하는것ㅇ느 birthSeason 에서 NULL 이 된다
주의 할점 birthMonth = NULL 이렇게 조건문을 쓸 수 없고 birthMont is NULL 이렇게 비교를 해야한다
반응형