Padding a number with leading zero

I am making a folder cabinet table where the user can add up to 30 levels of folders

The table structure looks like the attached.

If my next record was a child of folderID 3.  I would need the folderIDPath to equal 23.03.04 (4 would be the identity key of the new record going in)

DepartmentID.ParentID.FolderID  with leading zeros for ParentId/FolderID

Anyone have any idea how I can write this in SQL Server?


Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PortletPaulEE Topic AdvisorCommented:
this will pad a zero, 1 or 2 digit integer:

RIGHT('00' + CONVERT(varchar,[number_column],2) ,2)

change [number_column] to the column name needed
PortletPaulEE Topic AdvisorCommented:
if nulls get in the way try this

RIGHT('00' + ISNULL(CONVERT(varchar,[number_column],2),'00') ,2)
PortletPaulEE Topic AdvisorCommented:
this result:


by this query:

      RIGHT('00' + ISNULL(CONVERT(varchar,[FolderID]),'00') ,2)
    + '.'
    + RIGHT('00' + ISNULL(CONVERT(varchar,[DepartmentID]),'00') ,2)
    + '.'
    + RIGHT('00' + ISNULL(CONVERT(varchar,[ParentID]),'00') ,2)
from table1

    ([FolderID] int, [DepartmentID] int, [ParentID] int, [FolderIDPath] numeric, [PathLevel] int, [UserID] int, [FolderName] varchar(27))
    ([FolderID], [DepartmentID], [ParentID], [FolderIDPath], [PathLevel], [UserID], [FolderName])
    (1, 23, NULL, 23.01, 0, 132, 'Accounting'),
    (2, 23, 1, 23.02, 1, 132, 'My First Accounting Folder'),
    (3, 23, 1, 23.03, 2, 132, 'My Second Accounting Folder')

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.