Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 5 of 5 total
Thread Question about RAND
Fri, Jun 6 2008 2:32 PMPermanent Link

John
The Manual says about the SQL RAND function:
"The RAND function returns a random number"

I need do create a random password with a fixed length of 6 characters.  Is that possible
with SQL using the RAND function?
Fri, Jun 6 2008 3:06 PMPermanent Link

Roy Lambert

NLH Associates

Team Elevate Team Elevate

John


Your problem is that RAND(999999) could return anything from 0 to 999999 which would not be useful for your purposes. If you're running V4 you'd be better off writing your own function and calling that. Otherwise you could do something like


CAST(RAND(9) AS VARCHAR(1)) + CAST(RAND(9) AS VARCHAR(1))+CAST(RAND(9) AS VARCHAR(1))+CAST(RAND(9) AS VARCHAR(1))+CAST(RAND(9) AS VARCHAR(1))+CAST(RAND(9) AS VARCHAR(1))

or

SUBSTR(TRIM(CAST(RAND(999999) AS VARCHAR(6)) + TRIM(CAST(RAND(999999) AS VARCHAR(6)) +TRIM(CAST(RAND(999999) AS VARCHAR(6)) +TRIM(CAST(RAND(999999) AS VARCHAR(6)) +TRIM(CAST(RAND(999999) AS VARCHAR(6)) +TRIM(CAST(RAND(999999) AS VARCHAR(6)) ,1,6)


Roy Lambert
Fri, Jun 6 2008 3:51 PMPermanent Link

"Robert"

"John" <eydunl@post.olivant.fo> wrote in message
news:0412E3AE-689B-4CA6-9147-BF99848FF77F@news.elevatesoft.com...
> The Manual says about the SQL RAND function:
> "The RAND function returns a random number"
>
> I need do create a random password with a fixed length of 6 characters.
> Is that possible
> with SQL using the RAND function?
>

Are spaces allowed?

Robert

Fri, Jun 6 2008 5:17 PMPermanent Link

John
No Spaces are not allowed. Only numbers.

Roy, thanks for the reply Smile
Mon, Jun 9 2008 5:08 AMPermanent Link

"John Hay"
Roy

> CAST(RAND(9) AS VARCHAR(1)) + CAST(RAND(9) AS VARCHAR(1))+CAST(RAND(9) AS
VARCHAR(1))+CAST(RAND(9) AS VARCHAR(1))+CAST(RAND(9) AS
VARCHAR(1))+CAST(RAND(9) AS VARCHAR(1))
>
> or
>
> SUBSTR(TRIM(CAST(RAND(999999) AS VARCHAR(6)) + TRIM(CAST(RAND(999999) AS
VARCHAR(6)) +TRIM(CAST(RAND(999999) AS VARCHAR(6)) +TRIM(CAST(RAND(999999)
AS VARCHAR(6)) +TRIM(CAST(RAND(999999) AS VARCHAR(6))
+TRIM(CAST(RAND(999999) AS VARCHAR(6)) ,1,6)

How about RAND(899999)+100000 <bg>

John

Image