tags:

views:

63

answers:

3

I don't have much t-sql experience, but I came up with this function to pad 0's to the left of an nvarchar parameter passed in. The number of 0's padded to the left is based on the original length of the parameter passed in. I will explain each variable and then display my function. I tested it out and it does work, but I am curious if there are any refactorings or improvements I can make to it.

@number is the number passed into the function that will be padded

@result is the returned padded number -- the datatype for number is actually an nvarchar even though I call it number.

@maxLength is the maximum length all of the numbers must conform to. This is always 10.

@actualLength is the original length of the number passed into the function

@numOfZerosToPad is the @maxLength - @actualLength

As a side note, I have seen people do SELECT @variable = 1 and SET @variable = 1. Is there a difference to this or is it more a matter of taste?

Here is the function:

CREATE FUNCTION f_PadZerosToNumber
(
@number nvarchar(10)
)
RETURNS nvarchar(10)
AS
BEGIN

DECLARE @Result nvarchar(10)

    DECLARE @maxLength int

    SET @maxLength = 10

    DECLARE @actualLength int

    SET @actualLength = LEN(@number)

    DECLARE @numOfZerosToPad int

    SET @numOfZerosToPad = @maxLength - @actualLength

SET @Result = REPLICATE('0',@numOfZerosToPad) + @number

RETURN @Result

END
GO
+5  A: 

If you're definitely limited to ten and the padding is always zeros...

CREATE FUNCTION f_PadZerosToNumber (
    @Number nvarchar(10)
)
RETURNS nvarchar(10)
AS BEGIN
    RETURN RIGHT('0000000000' + @Number, 10)
END
GO
Dylan Beattie
Yes, the number must always be 10. Does it matter how long the original number is in this case?
Xaisoft
I was going to ask this as a separate question, but I guess I will just ask it here. With regards to the left and right functions, if the number + the '0000000000' is smaller than 10, it will return the original number plus the 0's to the left to make up 10 characters, correct? It is a little confusing because, you are calling the function RIGHT, but it is padding the numbers on to the left.
Xaisoft
Given '1111' as @Number, appending it to '0000000000' will result in '00000000001111'. Extracting the 10 rightmost characters will result in '0000001111', which appears to be what you're looking to achieve. That should answer both of your follow up questions, if you change '1111' to '22222' and repeat my example.
Ken White
@Ken, ok it makes sense now. Using right, it is basically grabbing the numbers starting from the right, but if I use left, it would grab the numbers starting from the left and count to 10, correct?
Xaisoft
You start with your input - say '1234'You add ten zeros to the beginning:'00000000001234'Then you take the TEN RIGHT-MOST CHARACTERS from the result, giving you '0000001234'Yes, it looks backwards. But it works.
Dylan Beattie
Thanks Dylan. After seeing your code, mine looks like a complete mess.
Xaisoft
I think using the word "extract" is better than pad because it makes it easier to understand.
Xaisoft
This can be made universal with `REPLICATE('0', @length)`
Tomalak
What do you mean by universal?
Xaisoft
It appears with replicate that I need to calculate the length of 0's to pad?
Xaisoft
@Xaisoft: Yes, using LEFT() would grab the characters starting from the left. By "universal", Tomalak means "more generic"; if you use REPLICATE() as he indicated, you can easily change from a final width of 10 to a different final width just by changing the parameter passed as @length.
Ken White
A: 

As a value-add, if you're trying to pad numbers only, consider converting them to INT and then using the following:

DECLARE @MyNumber INT, 
        @PadCount INT
SELECT  @MyNumber = 123,
        @PadCount = 8
SELECT REPLACE(STR(@MyNumber, @PadCount), ' ', '0')

Good luck!

Ed Altorfer
If I need to store these updated numbers as nvarchars back into an nvarchar column, that would be an extra step, correct?
Xaisoft
Yep—you could cast the result to NVARCHAR if you need to.
Ed Altorfer
+2  A: 

You could replace most of the code in your method with this

SELECT RIGHT(REPLICATE(@padChar,@size) + @number, @size)

You just need to set @padChar to '0', @size to 10 and @number to the number you're padding

How you go about that, hardcoding, function parameters, etc is up to you

Chad
Thanks for the tip. In my case, using just RIGHT seems to just work since I know the length must be 10.
Xaisoft