Is there a TSQL alphanumeric like IDENTITY column

2019-09-17 14:23发布

问题:

Is there an IDENTITY like column type that generates alphanumeric values? Like 023904F?

回答1:

YES, the uniqueidentifier column, but it is 36 chars in length, try this:

select newid()

output

------------------------------------
53F2103C-C357-429E-A0E8-2DC26666638F

(1 row(s) affected)

you can use it like:

select LEFT(newid(),7)

and get:

-------
50D0F58

(1 row(s) affected)

this will not be unique though.



回答2:

No: you have to write a function to do it for you. Or concatenate "F" to a number in a computed columns

previous questions:

SQLServer IDENTITY Column with text

Increasing Alphanumeric value in user defined function



回答3:

No - but you could always

  • create an INT IDENTITY column
  • add a computed column such as

    ALTER TABLE dbo.YourTable 
       ADD ProductID AS CAST(ID AS VARCHAR(8)) + 'F'
    

    or whatever it is you want to do to the ID to make it into your alphanumeric field