---------------------------------------LPAD--------------------------------------
CREATE FUNCTION FNS_LPAD
(
@pStr NVARCHAR(100), --대상문자열
@pFillSize INT, --대상문자열길이
@pFillStr NVARCHAR(100) --채울 문자
)
RETURNS NVARCHAR(200)
AS
BEGIN
DECLARE @rtnValue NVARCHAR(200)
SET @rtnValue = CASE
WHEN LEN(@pStr) >= @pFillSize THEN LEFT(@pStr,@pFillSize)
ELSE REPLICATE(@pFillStr, @pFillSize-LEN(@pStr))+@pStr
---------------------------------------RPAD--------------------------------------
CREATE FUNCTION FNS_RPAD
(
@pStr NVARCHAR(100), --대상문자열
@pFillSize INT, --대상문자열길이
@pFillStr NVARCHAR(100) --채울 문자
)
RETURNS NVARCHAR(200)
AS
BEGIN
DECLARE @rtnValue NVARCHAR(200)
SET @rtnValue = CASE
WHEN LEN(@pStr) >= @pFillSize THEN RIGHT(@pStr,@pFillSize)
ELSE @pStr + REPLICATE(@pFillStr, @pFillSize-LEN(@pStr))
END
RETURN @rtnValue
END
----------------------------------------------------------------------------------
여기서 알아보고 넘어가야할 함수 REPLICATE
SELECT REPLICATE('반복할문자열',횟수)
반복할 문자열을 해당 횟수만큼 반복한답니다...
ex) SELECT REPLICATE('0',5)
결과 : 00000
출처 : http://nuricle.tistory.com/entry/MS-SQL-LPAD-RPAD-%EB%94%B0%EB%9D%BC%ED%95%98%EA%B8%B0