0

I use this code to add leading zeroes.

SELECT RIGHT('HL000000'+ CONVERT(VARCHAR(10), @holrefno + 1), 10) AS 'refno' 

But when I tested it, let's say I'll make the reference number as 99 it would show up as HL00000100. I want to keep it as a 10 digit string in my database, could anyone refer me my mistake? Or is there any way I could fix this?

EDIT:

I would like to have an input where the string length will still be 10.

marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425

2 Answers2

0

You just have to remove the 'HL' part of the right function (therefore also limit your varchar to 8), and also add a '0' in case @holrefno is less than 9.

SELECT 'HL' + right('0000000'+ CONVERT(VARCHAR(8), @holrefno + 1), 8) AS 'refno'
Andrew
  • 7,049
  • 2
  • 32
  • 41
0

Below one is working even though @holrefno is null

declare @holrefno int
--set @holrefno = 56784

SELECT 'HL' + right('0000000'+ CONVERT(VARCHAR(8), @holrefno + 1), 8) AS 'refno'
  • That looks pretty much like the accepted answer. ;) I bet you meant `ISNULL(@holrefno, 0)`. – Andrew Aug 27 '15 at 18:30