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 2000 Forums
 SQL Server Development (2000)
 Random Numbers

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2001-12-21 : 09:41:04
Felipe Amorim writes "Since i didn't found anything in SQLTeam.com i am asking you guys directly.
How do i create a User Defined Function that generates an eight digit random number?

Every time i try to use the RAND function it simply gives an error (443)

I am runing an SQL Server 2000 on Windows 2000 Server.

Thanks"

sica
Posting Yak Master

143 Posts

Posted - 2001-12-21 : 09:55:07
You can use NEWID() function...

Sica

Go to Top of Page

scootermcfly
Yak Posting Veteran

66 Posts

Posted - 2001-12-21 : 09:57:00
Here is a quick sample you can use:

DECLARE @charx int
SELECT @charx = 0
SELECT @charx = convert(int, rand() * 254)
PRINT 'x is: ' + convert(char,@charx)

Scooter McFly


Go to Top of Page

sica
Posting Yak Master

143 Posts

Posted - 2001-12-21 : 10:19:21
Sorry, I didn't see that you want 8 digit random number.Here comes a another try:
SELECT SUBSTRING(CONVERT(VARCHAR,CONVERT(INT,rand()*1000000*(SELECT CONVERT(INT,SUBSTRING(CONVERT(VARCHAR,GETDATE(),13),22,3))))),1,8)

Sica



Edited by - sica on 12/21/2001 10:20:33
Go to Top of Page
   

- Advertisement -