-2

我需要创建一个给定长度的随机数字字符串

CREATE FUNCTION UniqConvert
(@calue as varchar(max),
@len as int)

输出应该是长度@len 并且每个输入应该是唯一的@value

我已经问过类似的问题: Create random string of digits T-SQL This one has different concept

4

2 回答 2

0

尝试这个

   select Cast(Round(Rand()*power(10,@Len),0) as Varchar(30))
于 2014-04-26T12:50:39.607 回答
0
declare @len int
set @len = 3
DECLARE @Random INT      
DECLARE @Upper INT      
DECLARE @Lower INT
SET @Lower = power(10,@Len-1) ---- The lowest random number      
SET @Upper = power(10,@Len )-1 ---- The highest random number      
SELECT @Random = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0) 

select  @Random
于 2014-04-26T17:26:37.123 回答