views:

154

answers:

3

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

+3  A: 

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.

KM
OK, but that doesn't get added to the table automatically, like an INT IDENTITY - you would have to define a DEFAULT CONSTRAINT on that column to specify `newid()` as default value - close, but not exactly the same, I'd say...
marc_s
Will the `LEFT(newid(),7)` result in a unique value like `IDENTITY` does?
Raj More
@marc_s, after reading the question again and your comment, I'm now not sure of what is being asked. Does the OP want a semi-random char(7) or a sequential alphanumeric, an if so, what are the "rules", leading zeros, 0-9 and a-z or what? OP mentions identity, but is this to be unique?
KM
@Raj More: good point - I doubt taking the first 7 chars off a GUID will be unique for too long a period of time!
marc_s
@Raj More, no, in the answer I say this will not be unique though. you'd need all the original 36 chars to be unique. Does the op want this to be unique? they mention `identity` but little other details??
KM
A: 

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

marc_s
this goes to what the OP wants, 6 numeric digits + 1 letter? or something else. Looks like all three answers are for a different question, yet the OP didn't specify which of the three questions they are after.
KM
@KM: yes, the question does leave quite a bit of room for interpretation.
marc_s
+2  A: 

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

gbn
+1, I now think the OP wants something like your second link `Increasing Alphanumeric value in user defined function`
KM