Tuesday 14 July 2015

Conver Number to string ( exp. 9 - 0009 or 9 - 09)

This Function convert integer to string with N number of ceros to the left. It only needs two parameters the integer value and the lenght of the string that you need.. 
Example: 
 Select dbo.NumberToString (9, 5) you get 00009
 Select dbo.NumberToString (7, 2) you get 07
;)

CREATE FUNCTION [dbo].[NumberToString] (@value int, @size INT)
RETURNS VARCHAR(20)
AS
BEGIN
    DECLARE @TMP VARCHAR (128);
    SET @TMP = CAST(@value AS VARCHAR(20));
    IF @TMP IS NULL
        SET @TMP=0;
   
    WHILE LEN(@TMP)< @SIZE
        SET @TMP = '0'+@TMP;
    RETURN @TMP;
END 

No comments:

Post a Comment