본문 바로가기

it-day/postgreSQL

PostgreSql의 문자열 추출/자르기(unnest/split_part)

예를 들어 어떤 테이블의 컬럼 값이 이와 같이 있다면

 

테스트 테이블 및 컬럼

테이블 : testtable

컬럼 : testcolumn

컬럼 데이터 : 전송 = ID: [1,2], 코드: KBCD, 명: 테스트, email 주소: test01@test.com

 

select unnest(string_to_array(SUBSTRING(xa. testcolumn FROM '\[(.*?)\]'), ',')) as test_id
         , split_part(split_part(xa. testcolumn , '명: ', 2), ',', 1) as test_nm
  from testtable xa

 

unnest(string_to_array(SUBSTRING(xa. testcolumn FROM '\[(.*?)\]'), ','))

 - [] 대괄호 안의 값 추출

 - 결과 : 1,2

 

split_part(split_part(xa. testcolumn , '명: ', 2), ',', 1)

split_part('문자열', '자를 문자', 위치)

전송 = ID: [1,2], 코드: KBCD, 명: 테스트, email 주소: test01@test.com 이 문자열에서 

 - split_part(xa. testcolumn , '명: ', 2) 결과 : 테스트 , email 주소: test01@test.com

 - split_part(split_part(xa. testcolumn , '명: ', 2), ',', 1) 결과 : 테스트 

 

 

'it-day > postgreSQL' 카테고리의 다른 글

ROW_NUMBER() OVER PARTITION BY  (0) 2024.04.29
PostgreSQL에서 JSON Type 컬럼 데이터 추출  (0) 2023.10.25
날짜 요일 구하기 (postgresql)  (0) 2023.10.18