Wednesday, 9 November 2016

Creating Unique 10 Character Strings

A nice way to make a unique 10 character string is to generate a GUID, strip out the hypehens, and take only the left / right 10 characters as follows:
select right(replace(CONVERT(varchar(255), NEWID()), '-',''),10)
 This produces no duplicates over 1 million rows, when tested as follows:
create table #tmpID(ID varchar(10))

declare @i int
set @i = 1
while @i <1000000
begin
       insert into #tmpID
       select right(replace(CONVERT(varchar(255), NEWID()), '-',''),10)
       set @i = @i+1
end

select ID, count(*) [No of IDs]
from #tmpID
group by ID
having count(*) >1

No comments:

Post a Comment

Updating massive amount of rows whilst avoiding blocking

The following SQL is a good means to split an update on a massive table into smaller chunks, whilst reducing blocking. The method is to upda...