| Author |
Topic |
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2009-01-17 : 07:36:45
|
| How to generate Random Numbers In Sql 2005...I know How to use NEWID() But I like to generate the random numbers of 8 digits n only numeric fields r there..........but dont know how to generate this one,,,,,,,,i tried only this ,,,,,but the result is always zero for me,,,,,select cast(cast(newid() as binary)as bigint)so any help will be useful,,,Thanks In Advance,,,,,, |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2009-01-17 : 07:54:59
|
| Thanks,,,,,,Let me take a Look At this.... |
 |
|
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2009-01-17 : 08:00:51
|
| Ohkk Its Seems Godd and i also found my Solution but one more thing,,,if i generate Randoms Number like this,,,,,select left(newid(),6)then if this...always give me unique num,bers,,,as i want,,mean alphabets and numbers mixx...i just want to confirm that it always gime me,,Unique,,,,,Thnaks In Advance,,,, |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-17 : 08:05:35
|
| welcome |
 |
|
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2009-01-17 : 08:11:41
|
| select left(newid(),8)tell Me if this always generate unique,,,,,,I just want to know about the surety that it always give me unique,,,, |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-17 : 08:15:59
|
quote: Originally posted by ashishashish Ohkk Its Seems Godd and i also found my Solution but one more thing,,,if i generate Randoms Number like this,,,,,select left(newid(),6)then if this...always give me unique num,bers,,,as i want,,mean alphabets and numbers mixx...i just want to confirm that it always gime me,,Unique,,,,,Thnaks In Advance,,,,
but here its not a number but alphanumeric value |
 |
|
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2009-01-18 : 01:11:09
|
| yeai know here is not only numbers but alphanumeric values well it goes well with me if it always provide me UNIQUE....Thanks,,,,it is always provide UNIQUE???? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-18 : 09:05:00
|
quote: Originally posted by ashishashish yeai know here is not only numbers but alphanumeric values well it goes well with me if it always provide me UNIQUE....Thanks,,,,it is always provide UNIQUE????
NEWID() will give you unique value always, but taking a part of it cant guarantee that extracted value will be unique. |
 |
|
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2009-01-18 : 10:59:01
|
| ohkkk thanks man??? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-18 : 11:38:57
|
| welcome |
 |
|
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2009-01-19 : 00:52:03
|
| Is there Any Another way from which i get always unique alphanumeric value of 8 digits only,,,,,,,,,,Any Suggestions that whats the logic behind this,,,,or any kind of suggestions ,,,,Thanks In Advance,,,, |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-19 : 02:20:19
|
try thisselect number/power(10,len(number)-8) as number from(select top 10000 abs(checksum(newid())) as number from sysobjects s1 cross join sysobjects s2) as T |
 |
|
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2009-01-19 : 02:42:31
|
quote: Originally posted by visakh16 try thisselect number/power(10,len(number)-8) as number from(select top 10000 abs(checksum(newid())) as number from sysobjects s1 cross join sysobjects s2) as T
When we Use This As like u stated above then it says Error Encountered when Divide by Zero but when we replace this part ,,,select top 10000 to any other means select top 100or select top 10then it goe s wellThanks For that.,,,,,I try to make u understand my situation as i like to registers user on my site so when each user get registered then a Unique AlphaNumeric or Numeric anyone value get generated as ourself to give it user as its Password?So any suggestions in this,,,,,Thanks,,,,,, |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-19 : 03:16:00
|
| [code]select number/nullif(power(10,len(number)-8),0) as number from(select top 10000 abs(checksum(newid())) as number from sysobjects s1 cross join sysobjects s2) as T[/code] |
 |
|
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2009-01-19 : 03:55:47
|
quote: Originally posted by visakh16
select number/nullif(power(10,len(number)-8),0) as number from(select top 10000 abs(checksum(newid())) as number from sysobjects s1 cross join sysobjects s2) as T
Yes It Works Perfectly Thanks For this......... |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-01-19 : 07:32:34
|
quote: Originally posted by ashishashish How to generate Random Numbers In Sql 2005...I know How to use NEWID() But I like to generate the random numbers of 8 digits n only numeric fields r there..........but dont know how to generate this one,,,,,,,,i tried only this ,,,,,but the result is always zero for me,,,,,select cast(cast(newid() as binary)as bigint)so any help will be useful,,,Thanks In Advance,,,,,,
binary should be varbinaryMadhivananFailing to plan is Planning to fail |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-01-19 : 07:38:03
|
quote: Originally posted by ashishashish Is there Any Another way from which i get always unique alphanumeric value of 8 digits only,,,,,,,,,,Any Suggestions that whats the logic behind this,,,,or any kind of suggestions ,,,,Thanks In Advance,,,,
If you want alphanumeric valuesdeclare @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 tselect @alpha_numeric Modified from http://sqlblogcasts.com/blogs/madhivanan/archive/2007/11/20/random-password-generator.aspxMadhivananFailing to plan is Planning to fail |
 |
|
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2009-01-19 : 10:02:18
|
quote: Originally posted by madhivanan
quote: Originally posted by ashishashish Is there Any Another way from which i get always unique alphanumeric value of 8 digits only,,,,,,,,,,Any Suggestions that whats the logic behind this,,,,or any kind of suggestions ,,,,Thanks In Advance,,,,
If you want alphanumeric valuesdeclare @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 tselect @alpha_numeric Modified from http://sqlblogcasts.com/blogs/madhivanan/archive/2007/11/20/random-password-generator.aspxMadhivananFailing to plan is Planning to fail
Ohhh Thanks Sir..........Its gr88 to get your help there its solved my problem i get numbers from this,,,,,,thanks for your support and help its gr8888,,,,Its work perfectly fine for me....U saved my DayThanks Again.... |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-01-20 : 01:21:18
|
quote: Originally posted by ashishashish
quote: Originally posted by madhivanan
quote: Originally posted by ashishashish Is there Any Another way from which i get always unique alphanumeric value of 8 digits only,,,,,,,,,,Any Suggestions that whats the logic behind this,,,,or any kind of suggestions ,,,,Thanks In Advance,,,,
If you want alphanumeric valuesdeclare @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 tselect @alpha_numeric Modified from http://sqlblogcasts.com/blogs/madhivanan/archive/2007/11/20/random-password-generator.aspxMadhivananFailing to plan is Planning to fail
Ohhh Thanks Sir..........Its gr88 to get your help there its solved my problem i get numbers from this,,,,,,thanks for your support and help its gr8888,,,,Its work perfectly fine for me....U saved my DayThanks Again....
You are welcome MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|