我需要创建一个给定长度的随机字符串
CREATE FUNCTION UniqConvert
(@calue as varchar(max),
@len as int)
输出的长度应为@len
并且每个输入@value
我已经问了类似的问题: Create random string of digits T-SQL 这个有不同的概念
答案 0 :(得分:0)
试试这个
select Cast(Round(Rand()*power(10,@Len),0) as Varchar(30))
答案 1 :(得分: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