MSSQL 계층형(Hierarchy) 및 숫자를 한글로 변환
MSSQL 뿐만 아니라 프로젝트를 하다보면 계층형(Hierarchy) 구조의 설계를 많이 봅니다. 가장 많이 접하는 대표적인 예가 부서 정보일거라고 생각되네요.
하여 오늘은 빈번하게 사용하는 MSSQL 계층형(Hierarchy) 및 숫자를 한글로 변환에 대해서 알아보겠습니다.
WITH Department_ (DeptId, DeptNm, Sort, ParentDeptId, Hierarchy)
AS
(
SELECT
DeptId, DeptNm, Sort, DeptId
,CONVERT(VARCHAR, DeptId) AS Hierarchy
FROM Department WITH(NOLOCK)
WHERE Depth = 0 -- 최상위 정보
UNION ALL
SELECT
A.DeptId, A.DeptNm, A.Sort, A.ParentDeptId
,CONVERT(VARCHAR, Hierarchy + ':' + CONVERT(VARCHAR, A.DeptId)) AS Hierarchy
FROM Department A WITH(NOLOCK)
INNER JOIN Department_ B ON A.ParentDeptId = B.DeptId
)
상기의 쿼리를 이용하면 어떠한 계층형도 다~ 뽑아집니다.ㅎ
(
@WON BIGINT
)
RETURNS VARCHAR(200)
AS
BEGIN
DECLARE
@INPUT_VALUE VARCHAR(50)
, @NOW_POSITION TINYINT
, @ACC_KOREAN_VALUE NVARCHAR(100)
, @NOW_POSITION_VALUE CHAR(1)
, @BUFFER_KOREAN_VALUE NVARCHAR(100)
, @SIGN NVARCHAR(50)
SELECT @INPUT_VALUE = CAST(@WON AS VARCHAR(50)), @NOW_POSITION = 0, @ACC_KOREAN_VALUE = '', @NOW_POSITION_VALUE =''
SET @SIGN = CASE WHEN @WON = 0 THEN '영' WHEN @WON > 0 THEN '' ELSE '마이너스' END
WHILE (1=1)
BEGIN
SELECT @NOW_POSITION = LEN(@INPUT_VALUE), @NOW_POSITION_VALUE = LEFT(@INPUT_VALUE, 1)
IF LEN(@INPUT_VALUE) = 0 BREAK
IF @NOW_POSITION_VALUE <> 0
BEGIN
SELECT
@ACC_KOREAN_VALUE =
@ACC_KOREAN_VALUE +
CASE @NOW_POSITION_VALUE
WHEN '0' THEN '영'
WHEN '1' THEN '일'
WHEN '2' THEN '이'
WHEN '3' THEN '삼'
WHEN '4' THEN '사'
WHEN '5' THEN '오'
WHEN '6' THEN '육'
WHEN '7' THEN '칠'
WHEN '8' THEN '팔'
WHEN '9' THEN '구'
END +
CASE @NOW_POSITION
WHEN '1' THEN ''
WHEN '2' THEN '십'
WHEN '3' THEN '백'
WHEN '4' THEN '천'
WHEN '5' THEN '만'
WHEN '6' THEN '십만'
WHEN '7' THEN '백만'
WHEN '8' THEN '천만'
WHEN '9' THEN '억'
WHEN '10' THEN '십억'
WHEN '11' THEN '백억'
WHEN '12' THEN '천억'
WHEN '13' THEN '조'
WHEN '14' THEN '십조'
WHEN '15' THEN '백조'
WHEN '16' THEN '천조'
WHEN '17' THEN '경'
WHEN '18' THEN '십경'
WHEN '19' THEN '백경'
WHEN '20' THEN '천경'
END
IF CHARINDEX ('만',@ACC_KOREAN_VALUE) > 0
BEGIN
WHILE (1=1)
BEGIN
IF ( CHARINDEX ('만',@ACC_KOREAN_VALUE) > 0 )
SELECT @BUFFER_KOREAN_VALUE = STUFF(@ACC_KOREAN_VALUE, CHARINDEX('만', @ACC_KOREAN_VALUE),1,'')
IF ( CHARINDEX ('만',@BUFFER_KOREAN_VALUE) = 0 )
BEGIN
BREAK;
END
ELSE
BEGIN
SELECT @ACC_KOREAN_VALUE = STUFF(@ACC_KOREAN_VALUE, CHARINDEX('만', @ACC_KOREAN_VALUE),1,'')
END
END
END
IF CHARINDEX ('억',@ACC_KOREAN_VALUE) > 0 BEGIN
WHILE (1=1)
BEGIN
IF ( CHARINDEX ('억',@ACC_KOREAN_VALUE) > 0 )
SELECT @BUFFER_KOREAN_VALUE = STUFF(@ACC_KOREAN_VALUE, CHARINDEX('억', @ACC_KOREAN_VALUE),1,'')
IF ( CHARINDEX ('억',@BUFFER_KOREAN_VALUE) = 0 )
BEGIN
BREAK;
END
ELSE
BEGIN
SELECT @ACC_KOREAN_VALUE = STUFF(@ACC_KOREAN_VALUE, CHARINDEX('억', @ACC_KOREAN_VALUE),1,'')
END
END
END
IF CHARINDEX ('조',@ACC_KOREAN_VALUE) > 0
BEGIN
WHILE (1=1)
BEGIN
IF ( CHARINDEX ('조',@ACC_KOREAN_VALUE) > 0 )
SELECT @BUFFER_KOREAN_VALUE = STUFF(@ACC_KOREAN_VALUE, CHARINDEX('조', @ACC_KOREAN_VALUE),1,'')
IF ( CHARINDEX ('조',@BUFFER_KOREAN_VALUE) = 0 )
BEGIN
BREAK;
END
ELSE
BEGIN
SELECT @ACC_KOREAN_VALUE = STUFF(@ACC_KOREAN_VALUE, CHARINDEX('조', @ACC_KOREAN_VALUE),1,'')
END
END
END
IF CHARINDEX ('경',@ACC_KOREAN_VALUE) > 0
BEGIN
WHILE (1=1)
BEGIN
IF ( CHARINDEX ('경',@ACC_KOREAN_VALUE) > 0 )
SELECT @BUFFER_KOREAN_VALUE = STUFF(@ACC_KOREAN_VALUE, CHARINDEX('경', @ACC_KOREAN_VALUE),1,'')
IF ( CHARINDEX ('경',@BUFFER_KOREAN_VALUE) = 0 )
BEGIN
BREAK;
END
ELSE
BEGIN
SELECT @ACC_KOREAN_VALUE = STUFF(@ACC_KOREAN_VALUE, CHARINDEX('경', @ACC_KOREAN_VALUE),1,'')
END
END
END
END
SELECT @INPUT_VALUE = SUBSTRING(@INPUT_VALUE,2,LEN(@INPUT_VALUE)-1)
END
RETURN @SIGN + @ACC_KOREAN_VALUE;
END
함수는 속도를 느리게한다는것 알고 계시줘...ㅎ
너무 많은 데이터에서 함수를 호출하지 않게 주의해서 활용하시면 유익한 함수가 될것 같네요...ㅎ
이상으로 MSSQL 계층형(Hierarchy) 및 숫자를 한글로 변환에 대해서 알아보았습니다.
끝까지 읽어주셔서 감사합니다.^^
하여 오늘은 빈번하게 사용하는 MSSQL 계층형(Hierarchy) 및 숫자를 한글로 변환에 대해서 알아보겠습니다.
▶ 계층형(Hierarchy) 구조
아래 쿼리는 부서에 관한 정보를 계층형으로 추출하는 쿼리 입니다.WITH Department_ (DeptId, DeptNm, Sort, ParentDeptId, Hierarchy)
AS
(
SELECT
DeptId, DeptNm, Sort, DeptId
,CONVERT(VARCHAR, DeptId) AS Hierarchy
FROM Department WITH(NOLOCK)
WHERE Depth = 0 -- 최상위 정보
UNION ALL
SELECT
A.DeptId, A.DeptNm, A.Sort, A.ParentDeptId
,CONVERT(VARCHAR, Hierarchy + ':' + CONVERT(VARCHAR, A.DeptId)) AS Hierarchy
FROM Department A WITH(NOLOCK)
INNER JOIN Department_ B ON A.ParentDeptId = B.DeptId
)
상기의 쿼리를 이용하면 어떠한 계층형도 다~ 뽑아집니다.ㅎ
▶ 숫자를 한글로 변환해주는 함수
ALTER FUNCTION [dbo].[fnMoneyString](
@WON BIGINT
)
RETURNS VARCHAR(200)
AS
BEGIN
DECLARE
@INPUT_VALUE VARCHAR(50)
, @NOW_POSITION TINYINT
, @ACC_KOREAN_VALUE NVARCHAR(100)
, @NOW_POSITION_VALUE CHAR(1)
, @BUFFER_KOREAN_VALUE NVARCHAR(100)
, @SIGN NVARCHAR(50)
SELECT @INPUT_VALUE = CAST(@WON AS VARCHAR(50)), @NOW_POSITION = 0, @ACC_KOREAN_VALUE = '', @NOW_POSITION_VALUE =''
SET @SIGN = CASE WHEN @WON = 0 THEN '영' WHEN @WON > 0 THEN '' ELSE '마이너스' END
WHILE (1=1)
BEGIN
SELECT @NOW_POSITION = LEN(@INPUT_VALUE), @NOW_POSITION_VALUE = LEFT(@INPUT_VALUE, 1)
IF LEN(@INPUT_VALUE) = 0 BREAK
IF @NOW_POSITION_VALUE <> 0
BEGIN
SELECT
@ACC_KOREAN_VALUE =
@ACC_KOREAN_VALUE +
CASE @NOW_POSITION_VALUE
WHEN '0' THEN '영'
WHEN '1' THEN '일'
WHEN '2' THEN '이'
WHEN '3' THEN '삼'
WHEN '4' THEN '사'
WHEN '5' THEN '오'
WHEN '6' THEN '육'
WHEN '7' THEN '칠'
WHEN '8' THEN '팔'
WHEN '9' THEN '구'
END +
CASE @NOW_POSITION
WHEN '1' THEN ''
WHEN '2' THEN '십'
WHEN '3' THEN '백'
WHEN '4' THEN '천'
WHEN '5' THEN '만'
WHEN '6' THEN '십만'
WHEN '7' THEN '백만'
WHEN '8' THEN '천만'
WHEN '9' THEN '억'
WHEN '10' THEN '십억'
WHEN '11' THEN '백억'
WHEN '12' THEN '천억'
WHEN '13' THEN '조'
WHEN '14' THEN '십조'
WHEN '15' THEN '백조'
WHEN '16' THEN '천조'
WHEN '17' THEN '경'
WHEN '18' THEN '십경'
WHEN '19' THEN '백경'
WHEN '20' THEN '천경'
END
IF CHARINDEX ('만',@ACC_KOREAN_VALUE) > 0
BEGIN
WHILE (1=1)
BEGIN
IF ( CHARINDEX ('만',@ACC_KOREAN_VALUE) > 0 )
SELECT @BUFFER_KOREAN_VALUE = STUFF(@ACC_KOREAN_VALUE, CHARINDEX('만', @ACC_KOREAN_VALUE),1,'')
IF ( CHARINDEX ('만',@BUFFER_KOREAN_VALUE) = 0 )
BEGIN
BREAK;
END
ELSE
BEGIN
SELECT @ACC_KOREAN_VALUE = STUFF(@ACC_KOREAN_VALUE, CHARINDEX('만', @ACC_KOREAN_VALUE),1,'')
END
END
END
IF CHARINDEX ('억',@ACC_KOREAN_VALUE) > 0 BEGIN
WHILE (1=1)
BEGIN
IF ( CHARINDEX ('억',@ACC_KOREAN_VALUE) > 0 )
SELECT @BUFFER_KOREAN_VALUE = STUFF(@ACC_KOREAN_VALUE, CHARINDEX('억', @ACC_KOREAN_VALUE),1,'')
IF ( CHARINDEX ('억',@BUFFER_KOREAN_VALUE) = 0 )
BEGIN
BREAK;
END
ELSE
BEGIN
SELECT @ACC_KOREAN_VALUE = STUFF(@ACC_KOREAN_VALUE, CHARINDEX('억', @ACC_KOREAN_VALUE),1,'')
END
END
END
IF CHARINDEX ('조',@ACC_KOREAN_VALUE) > 0
BEGIN
WHILE (1=1)
BEGIN
IF ( CHARINDEX ('조',@ACC_KOREAN_VALUE) > 0 )
SELECT @BUFFER_KOREAN_VALUE = STUFF(@ACC_KOREAN_VALUE, CHARINDEX('조', @ACC_KOREAN_VALUE),1,'')
IF ( CHARINDEX ('조',@BUFFER_KOREAN_VALUE) = 0 )
BEGIN
BREAK;
END
ELSE
BEGIN
SELECT @ACC_KOREAN_VALUE = STUFF(@ACC_KOREAN_VALUE, CHARINDEX('조', @ACC_KOREAN_VALUE),1,'')
END
END
END
IF CHARINDEX ('경',@ACC_KOREAN_VALUE) > 0
BEGIN
WHILE (1=1)
BEGIN
IF ( CHARINDEX ('경',@ACC_KOREAN_VALUE) > 0 )
SELECT @BUFFER_KOREAN_VALUE = STUFF(@ACC_KOREAN_VALUE, CHARINDEX('경', @ACC_KOREAN_VALUE),1,'')
IF ( CHARINDEX ('경',@BUFFER_KOREAN_VALUE) = 0 )
BEGIN
BREAK;
END
ELSE
BEGIN
SELECT @ACC_KOREAN_VALUE = STUFF(@ACC_KOREAN_VALUE, CHARINDEX('경', @ACC_KOREAN_VALUE),1,'')
END
END
END
END
SELECT @INPUT_VALUE = SUBSTRING(@INPUT_VALUE,2,LEN(@INPUT_VALUE)-1)
END
RETURN @SIGN + @ACC_KOREAN_VALUE;
END
함수는 속도를 느리게한다는것 알고 계시줘...ㅎ
너무 많은 데이터에서 함수를 호출하지 않게 주의해서 활용하시면 유익한 함수가 될것 같네요...ㅎ
이상으로 MSSQL 계층형(Hierarchy) 및 숫자를 한글로 변환에 대해서 알아보았습니다.
끝까지 읽어주셔서 감사합니다.^^
댓글
댓글 쓰기