SQLServer IDENTITY column with text

How to create an IDENTITY column in SQLServer with text in a column?

Example:

ABCD-987065
ABCD-987066
ABCD-987067

+6
source share
7 answers

In addition to the other answers, you can create a computed column in a table to provide what you are asking for.

CREATE TABLE dbo.MyTable
(
    Id int NOT NULL PRIMARY KEY,
    CombinedId AS 'ABCD-' + CAST(Id as varchar(16)) 
)

Or:

CREATE TABLE dbo.MyTable
(
    Id int NOT NULL PRIMARY KEY,
    PrefixField varchar(16),
    CombinedId AS PrefixField + CAST(Id as varchar(16)) 
)

(Your question does not indicate whether the prefix should be fixed or not ...)

+14
source

You do not need to use the IDENTITY column, but create the identifiers / rows yourself.

IDENTITY , - / db.

(.. , "ABCD-" ), .

+1

. int bigint.

0

, . "" .

0

, Select , :

Select 'ABCD-' + Cast(IdColumn as varchar) as IdColumn From MyTable Where (....);

, IdColumn "IDENTITY" "true". SQL Server Management Studio, .

"ABCD" , , :

Select PrefixField + '-' + Cast(IdColumn as varchar) as IdColumn From MyTable Where (....);

, , , . , , .

0

...

-

0

All Articles