SELECT '010 - 1234 - 5678' AS HP FROM DUAL UNION ALL
SELECT '010)1234-5678' AS HP FROM DUAL UNION ALL
SELECT '010/3333/3333' AS HP FROM DUAL UNION ALL
SELECT '010 4372 1234' AS HP FROM DUAL UNION ALL
SELECT '016 123 1234' AS HP FROM DUAL
▶ 최종 결과물들을 보고 단계단계 보도록 하자.
WITH TMP AS
(
SELECT REGEXP_REPLACE(REGEXP_REPLACE(HP, '[[:punct:]]'), '( ){1,}', '') AS HP
FROM (
SELECT '010 - 1234 - 5678' AS HP FROM DUAL UNION ALL
SELECT '010)1234-5678' AS HP FROM DUAL UNION ALL
SELECT '010/3333/3333' AS HP FROM DUAL UNION ALL
SELECT '010 4372 1234' AS HP FROM DUAL UNION ALL
SELECT '016 123 1234' AS HP FROM DUAL
)
)
SELECT HP
, REGEXP_REPLACE(HP, '(.{3})(.*)(.{4})', '\1-\2-\3') HP1
, REGEXP_REPLACE(HP, '(.{3})(.*)(.{4})', '\1-****-\3') HP2
, REGEXP_REPLACE(REGEXP_REPLACE(HP, '(.{3})(.*)(.{4})', '\1-\2-\3'), '-(.*)-', '-'||LPAD('*',
LENGTH(REGEXP_REPLACE(HP, '(.{3})(.*)(.{4})', '\2')), '*')||'-'
) HP3
, RPAD(SUBSTR(HP, 0, 3), 3+LENGTH(REGEXP_REPLACE(HP, '(.{3})(.*)(.{4})', '\2')), '*')
||
SUBSTR(HP, 4 + LENGTH(REGEXP_REPLACE(HP, '(.{3})(.*)(.{4})', '\2')), LENGTH(HP)) HP4
, REGEXP_REPLACE(
RPAD(SUBSTR(HP, 0, 3), 3+LENGTH(REGEXP_REPLACE(HP, '(.{3})(.*)(.{4})', '\2')), '*')
||
SUBSTR(HP, 4 + LENGTH(REGEXP_REPLACE(HP, '(.{3})(.*)(.{4})', '\2')), LENGTH(HP)
), '(.{3})(.*)(.{4})', '\1-\2-\3') HP5
FROM TMP;
- Case 1 : 핸드폰번호에 특수 문자나 공백 등이 포함되어 데이터가 넘어 왔을때 특수문자 및 공백 제거
SELECT REGEXP_REPLACE(REGEXP_REPLACE(HP, '[[:punct:]]'), '( ){1,}', '') AS HP
FROM
(
SELECT '010 - 1234 - 5678' AS HP FROM DUAL UNION ALL
SELECT '010)1234-5678' AS HP FROM DUAL UNION ALL
SELECT '010/3333/3333' AS HP FROM DUAL UNION ALL
SELECT '010 4372 1234' AS HP FROM DUAL UNION ALL
SELECT '016 123 1234' AS HP FROM DUAL
)
- Case 2 : 핸드폰 번호를 구분자(-)로 구분지어 표현 하여 준다.
1) 총 10자리 핸드폰 번호 : 000-000-0000
2) 총 11자리 핸드폰 번호 : 000-0000-0000
WITH TMP AS
(
SELECT REGEXP_REPLACE(REGEXP_REPLACE(HP, '[[:punct:]]'), '( ){1,}', '') AS HP
FROM (
SELECT '010 - 1234 - 5678' AS HP FROM DUAL UNION ALL
SELECT '010)1234-5678' AS HP FROM DUAL UNION ALL
SELECT '010/3333/3333' AS HP FROM DUAL UNION ALL
SELECT '010 4372 1234' AS HP FROM DUAL UNION ALL
SELECT '016 123 1234' AS HP FROM DUAL
)
)
SELECT REGEXP_REPLACE(HP, '(.{3})(.*)(.{4})', '\1-\2-\3') HP
FROM TMP;
- Case 3 : 가운데 부분 3~4 자리 *로 마스킹 처리
WITH TMP AS
(
SELECT REGEXP_REPLACE(REGEXP_REPLACE(HP, '[[:punct:]]'), '( ){1,}', '') AS HP
FROM (
SELECT '010 - 1234 - 5678' AS HP FROM DUAL UNION ALL
SELECT '010)1234-5678' AS HP FROM DUAL UNION ALL
SELECT '010/3333/3333' AS HP FROM DUAL UNION ALL
SELECT '010 4372 1234' AS HP FROM DUAL UNION ALL
SELECT '016 123 1234' AS HP FROM DUAL
)
)
SELECT REGEXP_REPLACE(REGEXP_REPLACE(HP, '(.{3})(.*)(.{4})', '\1-\2-\3'), '-(.*)-', '-'||LPAD('*',
LENGTH(REGEXP_REPLACE(HP, '(.{3})(.*)(.{4})', '\2')), '*')||'-'
) HP
FROM TMP;
- Case4 : 또다른 방법으로도 마스킹해보자.
1) 구분자(-) 없는 경우
2) 구분자(-) 있는 경우
WITH TMP AS
(
SELECT REGEXP_REPLACE(REGEXP_REPLACE(HP, '[[:punct:]]'), '( ){1,}', '') AS HP
FROM (
SELECT '010 - 1234 - 5678' AS HP FROM DUAL UNION ALL
SELECT '010)1234-5678' AS HP FROM DUAL UNION ALL
SELECT '010/3333/3333' AS HP FROM DUAL UNION ALL
SELECT '010 4372 1234' AS HP FROM DUAL UNION ALL
SELECT '016 123 1234' AS HP FROM DUAL
)
)
SELECT RPAD(SUBSTR(HP, 0, 3), 3+LENGTH(REGEXP_REPLACE(HP, '(.{3})(.*)(.{4})', '\2')), '*')
||
SUBSTR(HP, 4 + LENGTH(REGEXP_REPLACE(HP, '(.{3})(.*)(.{4})', '\2')), LENGTH(HP)) HP1
, REGEXP_REPLACE(
RPAD(SUBSTR(HP, 0, 3), 3+LENGTH(REGEXP_REPLACE(HP, '(.{3})(.*)(.{4})', '\2')), '*')
||
SUBSTR(HP, 4 + LENGTH(REGEXP_REPLACE(HP, '(.{3})(.*)(.{4})', '\2')), LENGTH(HP)
), '(.{3})(.*)(.{4})', '\1-\2-\3') HP2
FROM TMP;
이외에도 여러 방법으로 핸드폰 번호 마스킹 처리하는 방법을 만들어 낼 수 있다.
본인이 원하는 방법으로 사용하던, 참고해서 사용하던 대량 트래픽 하에서만 수행하지 않으면 상관 없지 않을까?