[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1163
  • Last Modified:

Generate newid() uniqueidentifier as a char(16) field

I need to generate a 16 digits universal unique alphanumeric number.
I tried to do it using newid() function  and save it in a char(16) field but it doesn't work.
Any other ideas?
1 Solution
Gustavo Perez BuenrostroCommented:

declare @sMyOwnId char(16)
select @sMyOwnId = substring(replace(cast(newid() as varchar(36)),'-',''),1,16)
select @sMyOwnId

PD: I think you could create your own unique-value generator routine based on GETDATE and RAND functions. Let me know if you need a better approach for creating unique values.
mordiAuthor Commented:
To use the full universal capability of the newid function, i have to convert every 2 digits(hex) of the 32 that the function returns, to one digit, and thus converting to a 16 digit guid.
Can you help?

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

Tackle projects and never again get stuck behind a technical roadblock.
Join Now