설문조사
PostgreSQL/PPAS 관련 듣고 싶은 교육은


총 게시물 94건, 최근 0 건
   

IP ADDRESS 를 DOT 기존으로 분리하기

글쓴이 : PostgresDBA 날짜 : 2012-12-24 (월) 16:37 조회 : 7649
오라클에서는 아래와 같은 쿼리로 IP adress 를 DOT(.) 기준으로 분리할수 있습니다.

SCOTT@ORA11GR2>
WITH X
       AS (SELECT '192.168.0.1' ip FROM DUAL)
  SELECT
    ip,
    SUBSTR(ip, 1, INSTR(ip, '.') - 1) a,
    SUBSTR(ip, INSTR(ip, '.') + 1, INSTR(ip, '.', 1, 2) - INSTR(ip, '.') - 1) b,
    SUBSTR(ip, INSTR(ip, '.', 1, 2) + 1, INSTR(ip, '.', 1, 3) - INSTR(ip, '.', 1, 2) - 1) c,
    SUBSTR(ip, INSTR(ip, '.', 1, 3) + 1) d
  FROM X;

IP          A                      B                      C                      D
----------- ---------------------- ---------------------- ---------------------- ----------------------
192.168.0.1 192                    168                    0                      1

16:30:37 SCOTT@ORA11GR2>

PostgreSQL 에서는 어떻게 구현할수 있을까요?
split_part 함수를 이용하면 됩니다.

scott@cloud-00:5432:scottdb] 
SQL> WITH X AS 
(
SELECT CAST('192.168.0.1' AS TEXT) ip
)
SELECT ip,
split_part(ip,'.',1) AS a,
split_part(ip,'.',2) AS b,
split_part(ip,'.',3) AS c,
split_part(ip,'.',4) AS d
FROM X;
     ip      |  a  |  b  | c | d 
-------------+-----+-----+---+---
 192.168.0.1 | 192 | 168 | 0 | 1
(1 row)

참고로 아래와 같이 종으로 보여지게 할수도 있습니다. 그것도 아주쉽게 말이죠^^

scott@[local]:5432:scottdb] 
SQL> select unnest(string_to_array('192.168.0.1','.')) as x;
  x  
-----
 192
 168
 0
 1
(4 rows)

scott@[local]:5432:scottdb] 
SQL> 


PostgresDBA 2014-11-13 (목) 16:30
* string_to_array  유용한 샘플
postgres=#  select (string_to_array(version(),' '));
                                                  string_to_array                                                 
---------------------------------------------------------------------------------------------------------------------
 {PostgreSQL,9.3.2,on,"x86_64-unknown-linux-gnu,",compiled,by,gcc,(GCC),4.1.2,20080704,(Red,Hat,"4.1.2-52),",64-bit}
(1 row)

postgres=#  select (string_to_array(version(),' '))[1];
 string_to_array
-----------------
 PostgreSQL
(1 row)

postgres=#  select (string_to_array(version(),' '))[2];
 string_to_array
-----------------
 9.3.2
(1 row)
댓글주소
   

postgresdba.com