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?

Thanks.



filecabinets.xlsx
sherbug1015Asked:
Who is Participating?
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.

PortletPaulfreelancerCommented:
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
0
PortletPaulfreelancerCommented:
if nulls get in the way try this

RIGHT('00' + ISNULL(CONVERT(varchar,[number_column],2),'00') ,2)
0
PortletPaulfreelancerCommented:
this result:

01.23.00
02.23.01
03.23.01

by this query:

select
      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

see: http://sqlfiddle.com/#!3/117aa/4
CREATE TABLE Table1
    ([FolderID] int, [DepartmentID] int, [ParentID] int, [FolderIDPath] numeric, [PathLevel] int, [UserID] int, [FolderName] varchar(27))
;
    
INSERT INTO Table1
    ([FolderID], [DepartmentID], [ParentID], [FolderIDPath], [PathLevel], [UserID], [FolderName])
VALUES
    (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

1

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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.