[SQL] Case, SUBSTRING, SUBSTRING_INDEX

CountryGirlยท2023๋…„ 6์›” 3์ผ
0

SQL

๋ชฉ๋ก ๋ณด๊ธฐ
7/7
post-thumbnail

๐Ÿ“Œ Case

ํŠน์ • ์กฐ๊ฑด์— ๋”ฐ๋ผ ๋ฐ์ดํ„ฐ๋ฅผ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ •๋ฆฌํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

case
when ์กฐ๊ฑด then ์กฐ๊ฑด์— ์ฐธ์ผ ๊ฒฝ์šฐ
else ์กฐ๊ฑด์— ์ฐธ์ด ์•„๋‹ ๊ฒฝ์šฐ
end

(์˜ˆ์‹œ)

์ˆ˜ํ•™ ์ ์ˆ˜๊ฐ€ 70์  ์ด์ƒ์ด๋ฉด "A-CLASS", 70์ ๋ณด๋‹ค ๋‚ฎ์œผ๋ฉด "B-CLASS"๋กœ ํ‘œ์‹œํ•˜๋ ค๊ณ  ํ•œ๋‹ค.

select subject, score,
case
when score >= 70 then 'A-CLASS' 
else 'B-CLASS'
END as 'CLASS'
from MATH; 

๐Ÿ“Œ SUBSTRING

๋ฌธ์ž์—ด์„ ์›ํ•˜๋Š” ๊ธธ์ด๋งŒํผ ์ชผ๊ฐ ๋‹ค.

SUBSTRING(๋ฌธ์ž์—ด ํ•„๋“œ, ์‹œ์ž‘์ธ๋ฑ์Šค, ๋ถ€ํ„ฐ ๋ช‡ ๊ฐœ) 

(์˜ˆ์‹œ)

A_table

DAYNUMBER
2021-09-12123123
2019-04-244353123
2021-11-1054676
1999-01-022435346
2021-07-29789456
2023-12-3018971
2000-05-05621587
......
......
......
select SUBSTRING(DAY, 1, 4) as A_YEAR from A_table 
A_YEAR
2021
2019
2021
1999
2021
2023
2000
...
...
...

๐Ÿ“Œ SUBSTRING_INDEX

๋ฌธ์ž์—ด์„ ์–ด๋– ํ•œ ๊ธฐ์ค€์œผ๋กœ ์ชผ๊ฐœ๊ณ  ์›ํ•˜๋Š” ์ธ๋ฑ์Šค ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ

SUBSTRING_INDEX(๋ฌธ์ž์—ด ํ•„๋“œ, ์ชผ๊ฐค ๊ธฐ์ค€, ๋ช‡ ๋ฒˆ์งธ ์ธ๋ฑ์Šค)

(์˜ˆ์‹œ)

B_table

emailNUMBER
abcd3479@naver.net123123
dfjkl888@daum.com4353123
kaka123@hanmail.com54676
dev09897@kakao.net2435346
hho124@naver.net789456
23nnb@naver.net18971
vvb45@daum.com621587
......
......
......
select SUBSTRING_INDEX(email, '@', 2) as domain from B_table 
domain
naver.net
daum.com
hanmail.com
kakao.net
naver.net
naver.net
daum.com
...
...
...
profile
๐Ÿ’ป๐ŸŒพ์‹œ๊ณจ์†Œ๋…€์˜ ์—‰๋ง์ง•์ฐฝ ๊ฐœ๋ฐœ ์„ฑ์žฅ์ผ์ง€๐ŸŒพ๐Ÿ’ป (2023.05.23 ~)

0๊ฐœ์˜ ๋Œ“๊ธ€