2

I have been using this code in Javascript but now would like to incorporate it into a stored procedure:

   self.test.testId =
      ('00000' + self.test.adminTestId).slice(-5) + "-" +
      ('00000' + self.test.userTestId).slice(-5) + "-" +
      ('00' + self.test.sequence).slice(-2);

What I would like to do is to accomplish the same with:

DECLARE @TestId VARCHAR(50)
DECLARE @AdminTestId INT
DECLARE @UserTestId  INT
DECLARE @Seq INT

Can anyone give me advice on how I can do padding and join these with "-" to go into @TestId

Alan2
  • 23,493
  • 79
  • 256
  • 450
  • Can you provide the output result you need please? That will help. – codea Mar 01 '16 at 08:57
  • You want to concat or add ? If concat the possible duplicate http://stackoverflow.com/questions/951320/how-to-concatenate-numbers-and-strings-to-format-numbers-in-t-sql – Pradip Mar 01 '16 at 09:09
  • Looking at the Javascrit I think the OP wants to concatenate *zero-padded* numbers to get a special number's format. Btw: I think the "Oh, it's a duplicate" reflex should - at least in most cases - not look on answers, which are **years** ago... – Shnugo Mar 01 '16 at 09:26

1 Answers1

1

Is it this you are looking for?

DECLARE @AdminTestId INT=3
DECLARE @UserTestId  INT=4
DECLARE @Seq INT=5

DECLARE @TestId VARCHAR(50)=REPLACE(STR(@AdminTestId,5),' ','0') + '-' + REPLACE(STR(@UserTestId,5),' ','0') + '-' + REPLACE(STR(@Seq,2),' ','0');

SELECT @TestId;

the result

00003-00004-05
Shnugo
  • 66,100
  • 9
  • 53
  • 114