카테고리 없음

20250207 SQL 3-1주차 : 필요한 문자 포맷이 다를 때, SQL로 가공하기

ddh1713 2025. 2. 7. 20:40
< 1 > REPLACE

 

[1] 사용 방법

replace(바꿀 컬럼, 현재 값, 바꿀 값)

 

[예시 1]

select restaurant_name "원래 상점명",
	replace(restaurant_name, 'Blue', 'Pink') "바뀐 상점명"
from food_orders
where restaurant_name like '%Blue Ribbon%'

 

[예시 2]

select addr,
	replace(addr, '문곡리', '문가리') "바뀐 주소"
from food_orders
where addr like '%문곡리%'

 

< 2 > SUBSTRING

 

[1] 함수명 : substring (substr)

 

[2] 사용 방법

substr(조회 할 컬럼, 시작 위치, 글자 수)

 

[예시 1]

select addr "원래 주소",
	substr(addr, 1, 2) "시도"
from food_orders
where addr like '%서울특별시%'

 

< 3 > CONCAT

 

 원하는 문자가 여러 컬럼에 있을 때, 하나로 합쳐서 업무에 필요한 형태로 만들 수 있다.

 

[1] 함수명 : concat

 

[2] 사용 방법

concat(붙이고 싶은 값1, 붙이고 싶은 값2, 붙이고 싶은 값3, ...)

 

[3] 붙일 수 있는 문자의 종류 : 컬럼, 한글, 영어, 숫자, 기타 특수문자

 

[예시 1]

select restaurant_name "원래 이름",
	addr "원래 주소",
    concat(restaurant_name, '-', cuisine_type) "음식타입별 음식점"),
	concat('[', substring(addr, 1, 2), ' ]', restaurant_name)
from food_orders
where addr like '%서울'