TSQL-Generate Unique String (6 Digit)

declare @UserCode nVarChar(6),@NextID Int=2

SET @UserCode =
RIGHT(‘000000’ +
CAST(
ABS(CAST(CAST(NEWID() AS BINARY(8)) AS BIGINT)) % 1000000
AS VARCHAR(6)
),
6);

Print @Usercode

DECLARE @rtnStr nVarChar(6)

set @rtnStr= RIGHT(‘000000’ +
CAST(
ABS(CAST(CAST(NEWID() AS BINARY(8)) AS BIGINT)) % 1000000
AS VARCHAR(6)
),
6);

Scroll to Top