/*
select [MASKING]('1','010-1234-1234')
select [MASKING]('1','01012341234--')
select [MASKING]('1','1234--')
select [MASKING]('2','홍길동')
select [MASKING]('3','test007@test.com')
select [MASKING]('4','12345')
select [MASKING]('5','서울시 영등포구 여의대여의동 122 abcd')
select [MASKING]('6','12345678')
select [MASKING]('8','12345')
select [MASKING]('9','1234-5678-1234-0123')
select [MASKING]('10','123-23-456789')
select [MASKING]('11','123-23-1234')
select [MASKING]('12','123456789')
select [MASKING]('16','1234567890abcdefghi1234567890abcdefghijk')
*/
CREATE FUNCTION [MASKING] (
@GUBUN VARCHAR(2),
@PARM VARCHAR(255)
)
RETURNS VARCHAR(255)
AS
BEGIN
DECLARE @RET VARCHAR(255)
-- 전화번호
IF @GUBUN = '1'
/*
SET @RET = CASE WHEN CHARINDEX('-',@PARM) > 0 THEN
CASE WHEN CHARINDEX('-', @PARM, CHARINDEX('-', @PARM) + 1) > 0 THEN
LEFT(@PARM, CHARINDEX('-', @PARM)) + REPLICATE( '*', CHARINDEX('-', @PARM, CHARINDEX('-', @PARM) + 1) - 1 - CHARINDEX('-', @PARM)) +
+ '-' + RIGHT(@PARM, LEN(@PARM) - CHARINDEX('-', @PARM, CHARINDEX('-', @PARM) + 1))
ELSE LEFT(@PARM, CHARINDEX('-', @PARM) - 1) + '-' + '****'
END
ELSE '****'
END
*/
SET @RET = CASE WHEN CHARINDEX('--',@PARM) > 0 THEN
CASE WHEN LEN(@PARM) > 8 THEN SUBSTRING(@PARM,1,4) + '****' + SUBSTRING(@PARM, 9, LEN(@PARM)-8)
WHEN LEN(@PARM) = 8 THEN SUBSTRING(@PARM,1,4) + '****'
WHEN LEN(@PARM) <= 4 THEN '****'
ELSE SUBSTRING(@PARM,1,LEN(@PARM)-4) + '****'
END
WHEN CHARINDEX('-',@PARM) > 0 THEN
CASE WHEN CHARINDEX('-', @PARM, CHARINDEX('-', @PARM) + 1) > 0 THEN
LEFT(@PARM, CHARINDEX('-', @PARM)) + REPLICATE( '*', CHARINDEX('-', @PARM, CHARINDEX('-', @PARM) + 1) - 1 - CHARINDEX('-', @PARM)) +
+ '-' + RIGHT(@PARM, LEN(@PARM) - CHARINDEX('-', @PARM, CHARINDEX('-', @PARM) + 1))
ELSE LEFT(@PARM, CHARINDEX('-', @PARM) - 1) + '-' + '****'
END
ELSE '****'
END
-- 이름(한글)
ELSE IF @GUBUN = '2'
SET @RET = CASE WHEN LEN(@PARM) < 2 THEN '*'
WHEN LEN(@PARM) = 2 THEN LEFT(@PARM, 1) + '*'
WHEN LEN(@PARM) > 2 THEN LEFT(@PARM, 1) + '*' + RIGHT(@PARM, 1)
END
-- EMAIL
ELSE IF @GUBUN = '3'
begin
-- SET @RET = REPLICATE('*', CHARINDEX('@', @PARM) - 1) + SUBSTRING(@PARM, CHARINDEX('@', @PARM), LEN(@PARM))
---- SET @RET = REPLICATE('*', 8) + '@' + REPLICATE('*', 8)
set @RET = [dbo].[fn_Split_IndexString](@PARM,'@',1)
if (LEN(@RET) > 4)
set @RET = '****' + RIGHT(@RET, LEN(@RET)-4) + '@' + [dbo].[fn_Split_IndexString](@PARM,'@',2)
else
set @RET = '**' + RIGHT(@RET, 2) + '@' + [dbo].[fn_Split_IndexString](@PARM,'@',2)
end
-- 우편번호
ELSE IF @GUBUN = '4'
--SET @RET = SUBSTRING(@PARM,1,4) + REPLICATE('*', 3)
SET @RET = case when len(@PARM) > 2 then left(@PARM, 3) + '**' else @PARM + '**' end
-- 주소
ELSE IF @GUBUN = '5'
--SET @RET = CASE SUBSTRING(@PARM, CHARINDEX(' ', @PARM, CHARINDEX(' ', @PARM, CHARINDEX(' ',@PARM) + 1) + 1) - 1, 1) WHEN '구'
-- THEN SUBSTRING(@PARM, 1, CHARINDEX(' ', @PARM, CHARINDEX(' ', @PARM, CHARINDEX(' ', @PARM) + 1) + 1)) + REPLICATE('*', 10)
-- ELSE SUBSTRING(@PARM, 1, CHARINDEX(' ', @PARM, CHARINDEX(' ', @PARM) + 1)) + REPLICATE('*', 10)
-- END
SET @RET = LEFT(@PARM, CHARINDEX(' ', @PARM, CHARINDEX(' ', @PARM, CHARINDEX(' ', @PARM, CHARINDEX(' ', @PARM) + 1) + 1))) + '*****'
-- 요양기관번호
ELSE IF @GUBUN = '6'
--SET @RET = '********'
set @RET = case when len(@PARM) > 3 then '****' + right(@PARM, LEN(@PARM)-4) else '****' + @PARM end
-- 주민번호
ELSE IF @GUBUN = '7'
SET @RET = LEFT(@PARM, 6) + '-*******'
-- 면허번호
ELSE IF @GUBUN = '8'
--SET @RET = REPLICATE('*', 5)
SET @RET = case when len(@PARM) > 2 then left(@PARM, 3) + '**' else @PARM + '**' end
-- 카드번호
ELSE IF @GUBUN = '9'
SET @RET = SUBSTRING(@PARM, 1, CHARINDEX('-', @PARM) - 1) + '-****-****-' +
RIGHT(@PARM, LEN(@PARM) - CHARINDEX('-', @PARM, CHARINDEX('-', @PARM, CHARINDEX('-', @PARM) + 1) + 1))
-- 계좌번호
ELSE IF @GUBUN = '10'
--SET @RET = SUBSTRING(@PARM, 1, LEN(@PARM) / 2) + REPLICATE('*', 10)
SET @RET = case when len(@PARM) > 5 then left(@PARM, len(@PARM)-5) + '****' else @PARM + '****' end
-- 사업자등록번호
ELSE IF @GUBUN = '11'
--SET @RET =
-- CASE
-- WHEN LEN(@PARM) > 0 THEN
-- LEFT(@PARM, CHARINDEX('-', @PARM))
-- + REPLICATE( '*', CHARINDEX('-', @PARM, CHARINDEX('-', @PARM) + 1) - 1 - CHARINDEX('-', @PARM)) +
-- + '-' + '*'+RIGHT (RIGHT(@PARM, CHARINDEX('-', REVERSE(@PARM))-1), LEN(RIGHT(@PARM, CHARINDEX('-', REVERSE(@PARM))-1))-1)
-- ELSE ''
-- END
set @RET = case when len(@PARM) > 6 then left(@PARM, 7) + '****' when len(@PARM) = 0 then '' else @PARM + '****' end
-- ID
ELSE IF @GUBUN = '12'
--SET @RET = LEFT(@PARM, LEN(@PARM)-2) + '**'
set @RET = case when len(@PARM) > 4 then '****' + right(@PARM, LEN(@PARM)-4)
when len(@PARM) > 1 then '**' + right(@PARM, LEN(@PARM)-2)
else '*' end
-- 생년월일 (yyyy-MM-dd)
ELSE IF @GUBUN = '13'
set @RET = [dbo].[fn_Split_IndexString](@PARM,'-',1) + '-**-**'
-- IP (ver4)
ELSE IF @GUBUN = '14'
set @RET = [dbo].[fn_Split_IndexString](@PARM,'.',1) + '.' + [dbo].[fn_Split_IndexString](@PARM,'.',2) + '.***.' + [dbo].[fn_Split_IndexString](@PARM,'.',4)
-- IP (ver6)
ELSE IF @GUBUN = '15'
set @RET = case when len(@PARM) > 34 then left(@PARM, 35) + '****' else @PARM + '****' end
-- DI
ELSE IF @GUBUN = '16'
set @RET = case when len(@PARM) > 6 then left(@PARM, 7) + '*****************' else @PARM + '*****************' end
-- 법인등록번호
ELSE IF @GUBUN = '17'
set @RET = case when len(@PARM) > 6 then left(@PARM, 7) + '*******' else @PARM + '*******' end
-- 이름(영문)
ELSE IF @GUBUN = '18'
set @RET = case when len(@PARM) > 2 then left(@PARM, 3) + '*****' else @PARM + '*****' end
RETURN @RET
END