Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Random Number

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

Posted - 2009-01-17 : 07:44:21
http://sqlblogcasts.com/blogs/madhivanan/archive/2007/10/10/generating-random-numbers-part-ii.aspx
http://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/generate-random-numbers.aspx
Go to Top of Page

ashishashish
Constraint Violating Yak Guru

408 Posts

Posted - 2009-01-17 : 07:54:59
Thanks,,,,,,
Let me take a Look At this....

Go to Top of Page

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,,,,
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-17 : 08:05:35
welcome
Go to Top of Page

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,,,,
Go to Top of Page

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
Go to Top of Page

ashishashish
Constraint Violating Yak Guru

408 Posts

Posted - 2009-01-18 : 01:11:09
yea
i 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????
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-18 : 09:05:00
quote:
Originally posted by ashishashish

yea
i 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.
Go to Top of Page

ashishashish
Constraint Violating Yak Guru

408 Posts

Posted - 2009-01-18 : 10:59:01
ohkkk thanks man???
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-18 : 11:38:57
welcome
Go to Top of Page

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,,,,
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-19 : 02:20:19
try this


select 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
Go to Top of Page

ashishashish
Constraint Violating Yak Guru

408 Posts

Posted - 2009-01-19 : 02:42:31
quote:
Originally posted by visakh16

try this


select 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 100
or select top 10

then it goe s well
Thanks 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,,,,,,





Go to Top of Page

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]
Go to Top of Page

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.........

Go to Top of Page

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 varbinary

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

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 values


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


Modified from http://sqlblogcasts.com/blogs/madhivanan/archive/2007/11/20/random-password-generator.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

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 values


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


Modified from http://sqlblogcasts.com/blogs/madhivanan/archive/2007/11/20/random-password-generator.aspx

Madhivanan

Failing 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 Day

Thanks Again....
Go to Top of Page

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 values


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


Modified from http://sqlblogcasts.com/blogs/madhivanan/archive/2007/11/20/random-password-generator.aspx

Madhivanan

Failing 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 Day

Thanks Again....


You are welcome

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -