日期:2014-05-19  浏览次数:20510 次

关于读取入位随机数的问题
select   cast((rand())   *10000000   as   int)
但是这种情况有可能,第一位是0的现象,如02547415,系统显示成7位,大家有没什么办法左补0,如果是第一二们是0,就补两个0

------解决方案--------------------
select right( '00000000 '+cast(cast((rand()) *10000000 as int) as varchar(20)),8)
------解决方案--------------------
select right( '00000000 '+ ltrim(cast((rand()) *10000000 as int)),8)
------解决方案--------------------
declare @str varchar(8)
declare @i int 0
while @i < 8 - len(cast(cast((rand()) *10000000 as int)) as varchar(8)))
begin
select @str += '0 '
@i++
end
if len(cast(cast((rand()) *10000000 as int)) as varchar(8))) <8
begin
select @str = @str + cast(cast((rand()) *10000000 as int)) as varchar(8))
end
else
select @str = cast(cast((rand()) *10000000 as int)) as varchar(8))