Surendra Sharma

Surendra Sharma

Search This Blog

Wednesday, June 5, 2013

Generate 8 digit random alphanumeric number as a initial password in SQL Server

-- generate 8 digit random alphanumeric number as a initial password

declare @alpha_numeric varchar(8)
      set @alpha_numeric=''
      select @alpha_numeric=@alpha_numeric+char(n) from
      (
      select top 8 number as n from master..spt_values
      where type='p' and (number between 48 and 57 or number between 65 and 90)
      order by newid()
      ) as t
      --select @alpha_numeric

OR

SELECT LEFT(CAST(NEWID() AS VARCHAR(100)), 8)

1 comment:

  1. Is there a way with this method to exclude certain characters (I am looking to exclude 1, l, i, 0 and O.

    ReplyDelete