Copy the code. Run the create and there is test script in the comments section you can use for testing.
There is a line commmented out that will limit the maximum size to 100 characters.
EX: SELECT dbo.DBA_fnAddLeadingZeros(1234,6)
will return '001234' for a TOTAL length of 6 characters.
This is especially helpful in data warehousing where you want to force all numbers to a fixed length such as day = '01' instead of 1... etc...
SELECT dbo.DBA_fnAddLeadingZeros(DATEPART(MONTH,GETDATE()),2) [MONTH]
, dbo.DBA_fnAddLeadingZeros(DATEPART(DAY,GETDATE()),2) [DAY]
, DATEPART(YEAR,GETDATE()) [YEAR]
, dbo.DBA_fnAddLeadingZeros(DATEPART(MONTH,GETDATE()),2) [HOUR]
, dbo.DBA_fnAddLeadingZeros(DATEPART(MINUTE,GETDATE()),2) [MINUTE]
, dbo.DBA_fnAddLeadingZeros(DATEPART(SECOND,GETDATE()),2) [SECOND]