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
 General SQL Server Forums
 New to SQL Server Programming
 Generating random numbers

Author  Topic 

AKP2008
Starting Member

45 Posts

Posted - 2008-12-29 : 04:33:00
Hi All,

I want to generate random 9 digit number with below conditions.

charat(1+3) positions - charat (5+7) positions = charat 9th position

For example my number is 123456788.

(1 + 3) - (5+7) = 8
So 9th character should be 8. This is my requirement.

How can i do this?

Please help me.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-29 : 04:36:03
http://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/generate-random-numbers.aspx

http://sqlblogcasts.com/blogs/madhivanan/archive/2007/10/10/generating-random-numbers-part-ii.aspx
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-12-29 : 08:51:35
[code]select number*10+abs((number/10000000+number/100000%10)-(number/1000%10+number/10%10)) as number from
(
select number/power(10,len(number)-case when len(number)>=8 then 8 else 0 end) as number from
(
select top 10000 abs(checksum(newid())) as number
from sysobjects s1 cross join sysobjects s2
) as T
) as t
[/code]

Madhivanan

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

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-12-29 : 10:50:48
If I understand the req correctly you are kind of generating a "check digit". What happens if the first 8 digits are: 21146281
(2+1) - (6+8) = -14

even if you ABS the result you end up with a 2 digit number so what should the ninth digit be?

Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -