Sql-server – In SQL Server, is there way to have a field where an auto-number is appended to the end of whatever value the field is set to

sql server

Sorry if that's not overly clear.

In SQL Server, can I have a field which I can place a string into, and then SQL Server appends an auto-number onto the end?

So for example, say I have column A.

  • If I set row 1 in column A to "Foo", the final value will be "Foo1"
  • Then if I set row 2 in column A to "Bar", the final value is "Bar2"

Is this possible?

Best Answer

Yes use a Calculated Value column. For example:

ALTER TABLE Table1 ADD ColumnB AS ColumnA + CAST(ID AS varchar(8)) PERSISTED

Assuming ID is where you're getting row 1 or row 2 value.