[Webinar] Streamline your web hosting managementRegister Today

x
  • 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?
0
mordi
Asked:
mordi
1 Solution
 
Gustavo Perez BuenrostroCommented:
Try:

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.
0
 
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?
0

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