본문 바로가기
MS-SQL

지정된 자리 수 만큼 앞자리에 특정 문자 채우기!!

by Hwoarang757 2013. 8. 26.

 

int형으로 선언된 넘버의 앞자리를 지정된 자리수 (예제에서는 총 4자리 ) 로 특정문자('0'이라는 문자를) 채우려 한다!!

 

While문으로 에제를 간단하게 만들어 보았다!!

 

DECLARE @IN_PARAM_PRODUCT_NUMBER INT

DECLARE @IN_PARAM_SUM_TEXT NVARCHAR(4)

 

 

SET @IN_PARAM_PRODUCT_NUMBER =  333

SET @IN_PARAM_SUM_TEXT = CONVERT(NVARCHAR(4),@IN_PARAM_PRODUCT_NUMBER)

 

 

WHILE LEN(@IN_PARAM_SUM_TEXT) <> 4

BEGIN

                                                                       IF LEN(@IN_PARAM_SUM_TEXT) <> 4

                                                                       BEGIN

                                                                                                                            SET @IN_PARAM_SUM_TEXT = '0' + @IN_PARAM_SUM_TEXT

                                                                       END

                                                                       ELSE

                                                                       BEGIN

                                                                                                                            BREAK

                                                                       END

                                                                      

END

 

 

 

SELECT @IN_PARAM_SUM_TEXT