本来我生成了一个五位数的随机数(数字),但现在需求改了,要生成五位数的字符随机数(数字或者字符)请问各位大峡用SQL SERVER怎么实现?

解决方案 »

  1.   

    select char(100*rand())+char(100*rand())+char(100*rand())+char(100*rand())+char(100*rand())
      

  2.   

    --如果樓主不覺得麻煩,可以試試這個.
    declare @t table(col varchar(01))
    insert into @t 
    select 'a' union select 'b' union select 'c' union select 'd' union select 'e' union select 'f' union select 'g' union select 'h' union select 'i' union select 'j' union select 'k' union select 'l' ....union select '8' union select '9'declare @sql varchar(10)
    set @sql=''
    select @sql=@sql+col from (select top 5 * from @t order by newid()) A 
    print @sql
      

  3.   

    http://community.csdn.net/Expert/topic/4872/4872530.xml?temp=3.197879E-02
      

  4.   

    declare @s varchar(36)
    set @s='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    print substring(@s,cast(rand()*36 as int)%36+1,1)
    +substring(@s,cast(rand()*36 as int)%36+1,1)
    +substring(@s,cast(rand()*36 as int)%36+1,1)
    +substring(@s,cast(rand()*36 as int)%36+1,1)
    +substring(@s,cast(rand()*36 as int)%36+1,1)
      

  5.   

    只需要字母:
    declare @s varchar(26)
    set @s='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    print substring(@s,cast(rand()*26 as int)%26+1,1)
    +substring(@s,cast(rand()*26 as int)%26+1,1)
    +substring(@s,cast(rand()*26 as int)%26+1,1)
    +substring(@s,cast(rand()*26 as int)%26+1,1)
    +substring(@s,cast(rand()*26 as int)%26+1,1)
      

  6.   

    把5作为一个可变参数输入
    declare @count int,@l_rand int,@l_str varchar(8),@flag int
    set @count = 5
    set @l_str = ''
    set @flag = 1
    while @flag <= @count
    begin
      set @l_rand = abs(checksum(newid()) % 37)
      set @l_str = @l_str + char(@l_rand + case when @l_rand<=10 then 48 when @l_rand>10 then 87 end)
      set @flag = @flag + 1
    end
    select @l_str
      

  7.   

    楼主要得到字母的话用playwarcraft(三角褲叉叉的頂點) 和 pbsql(风云) 的都可以风云的可改进一下,取随机大小写
    declare @s char(52)
    set @s='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
    print substring(@s,cast(rand()*52 as int)%52+1,1)
    +substring(@s,cast(rand()*52 as int)%52+1,1)
    +substring(@s,cast(rand()*52 as int)%52+1,1)
    +substring(@s,cast(rand()*52 as int)%52+1,1)
    +substring(@s,cast(rand()*52 as int)%52+1,1)