-2

What formula would I use in a persisted column so that I can add the two columns below together, the first must be padded to 5 characters:

  • ID (INT)
  • RefNum (STRING)

I went for format(ID,"00000") & RefNum, but it doesn't work, any ideas please?

aSystemOverload
  • 2,994
  • 18
  • 49
  • 73

2 Answers2

1

You could use something like this:

ALTER TABLE dbo.YourTable
ADD FormattedColumn AS RIGHT('00000' + CAST(ID AS VARCHAR(5)), 5) + RefNum PERSISTED

Basically, convert the ID to a VARCHAR(5), prepend it with 00000 and then grab the five right-most characters.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
0
SELECT RIGHT('00000' + CAST(ID AS VARCHAR(5)), 5) + RefNum
Curtis
  • 101,612
  • 66
  • 270
  • 352