Skip to content

Instantly share code, notes, and snippets.

@adamnoffie
Created March 11, 2021 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamnoffie/7fa044434cbc9a5f139d25d611a9287a to your computer and use it in GitHub Desktop.
Save adamnoffie/7fa044434cbc9a5f139d25d611a9287a to your computer and use it in GitHub Desktop.
-- ======================================================================================
-- Author: Adam Nofsinger
-- Created: 2013-04-29
-- Description: Utility to return an integer number as a string, padded to X many chars
-- with zeros to the left of the number. E.g. Pad(45, 5) = '00045'
-- ======================================================================================
CREATE FUNCTION [dbo].[Pad] (
@num integer,
@len integer = 3
) RETURNS varchar(100)
BEGIN
DECLARE @str varchar(100);
SET @str = CAST(@num AS varchar);
IF @len <= 10
SET @str = RIGHT('000000000'+ @str, @len);
ELSE
SET @str = REPLICATE('0', @len - LEN(@str)) + @str;
RETURN @str;
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment