开发者

How to get numeric random uniqueid in SQL Server

开发者 https://www.devze.com 2022-12-30 18:19 出处:网络
I am using SQL Server 2005, please开发者_Python百科 tell me how can I get 6 digits (unique numeric which should be random) value for every row in table.

I am using SQL Server 2005, please开发者_Python百科 tell me how can I get 6 digits (unique numeric which should be random) value for every row in table.

I have a field in table in which I need 6 digit numeric value (random and unique).

Please help me to do this.


SELECT ABS(CHECKSUM(NEWID())) % 999999

for a phone number:

SELECT RIGHT('000000' + CAST(ABS(CHECKSUM(NEWID())) % 999999 AS varchar(6)), 6)

NEWID is about as random as you can get in SQL Server.

However, if you want unique, you may as well start at 000000 and go to 999999. Any random generator will hit the birthday problem.

You can have unique or random that are reliable, but not both reliably

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号