patternsqlMinor
make random numbers for each row and column
Viewed 0 times
randomeachcolumnnumbersmakeforandrow
Problem
I will edit some fake data in my database. But if I will make random numbers for each row and column, it is not so random as I want it. The result can you see on image below.
Here is my code I use:
and this:
But both has the same result. I know, I must use an
I use SQL server with an express 2014 version.
Here is my code I use:
WITH x AS
(
SELECT mintwee, mineen, nul, pluseen, plustwee
FROM Topic
)
SELECT mintwee = CAST(RAND()*100 AS INT),
mineen = CAST(RAND()*100 AS INT),
nul = CAST(RAND()*100 AS INT),
pluseen = CAST(RAND()*100 AS INT),
plustwee = CAST(RAND()*100 AS INT)
FROM xand this:
WITH x AS
(
SELECT
mintwee = CAST(RAND()*100 AS INT),
mineen = CAST(RAND()*100 AS INT),
nul = CAST(RAND()*100 AS INT),
pluseen = CAST(RAND()*100 AS INT),
plustwee = CAST(RAND()*100 AS INT)
FROM Topic
)
SELECT mintwee, mineen, nul, pluseen, plustwee
FROM x ;But both has the same result. I know, I must use an
UPDATE statement, but this is only for try if the result is good. After it, I am going to use the UPDATE statement.I use SQL server with an express 2014 version.
Solution
Since there is no correlation between the table rows and the
One method to generated random values is with the
The expression can be simplified if you need only 32-bit integers. Additional examples:
RAND function, the expression is evaluated only once in the query.One method to generated random values is with the
NEWID() function. The example below generates random integer values between 0-99 range. The modulo value can be adjusted for a different range and you can use this basic technique to generate random values for dates, character, etc.WITH x AS
(
SELECT mintwee, mineen, nul, pluseen, plustwee
FROM Topic
)
SELECT mintwee = CAST(SUBSTRING(CAST(NEWID() AS binary(16)),1, 3) AS int) % 100,
mineen = CAST(SUBSTRING(CAST(NEWID() AS binary(16)),1, 3) AS int) % 100,
nul = CAST(SUBSTRING(CAST(NEWID() AS binary(16)),1, 3) AS int) % 100,
pluseen = CAST(SUBSTRING(CAST(NEWID() AS binary(16)),1, 3) AS int) % 100,
plustwee= CAST(SUBSTRING(CAST(NEWID() AS binary(16)),1, 3) AS int) % 100
FROM x;The expression can be simplified if you need only 32-bit integers. Additional examples:
--positive int
SELECT ABS(CHECKSUM(NEWID()));
--positive and negative int
SELECT CHECKSUM(NEWID());
--positive bigint
SELECT CAST(SUBSTRING(CAST(NEWID() AS binary(16)),1, 7) AS bigint);
--negative and positive bigint
SELECT CAST(SUBSTRING(CAST(NEWID() AS binary(16)),1, 8) AS bigint);
--dates between now and 1 year ago
SELECT DATEADD(day, -ABS(CHECKSUM(NEWID())) % 365, GETDATE());
--random uppercase character
SELECT CAST(CAST(65 + ABS(CHECKSUM(NEWID()) % 26) AS binary(1)) AS char(1));Code Snippets
WITH x AS
(
SELECT mintwee, mineen, nul, pluseen, plustwee
FROM Topic
)
SELECT mintwee = CAST(SUBSTRING(CAST(NEWID() AS binary(16)),1, 3) AS int) % 100,
mineen = CAST(SUBSTRING(CAST(NEWID() AS binary(16)),1, 3) AS int) % 100,
nul = CAST(SUBSTRING(CAST(NEWID() AS binary(16)),1, 3) AS int) % 100,
pluseen = CAST(SUBSTRING(CAST(NEWID() AS binary(16)),1, 3) AS int) % 100,
plustwee= CAST(SUBSTRING(CAST(NEWID() AS binary(16)),1, 3) AS int) % 100
FROM x;--positive int
SELECT ABS(CHECKSUM(NEWID()));
--positive and negative int
SELECT CHECKSUM(NEWID());
--positive bigint
SELECT CAST(SUBSTRING(CAST(NEWID() AS binary(16)),1, 7) AS bigint);
--negative and positive bigint
SELECT CAST(SUBSTRING(CAST(NEWID() AS binary(16)),1, 8) AS bigint);
--dates between now and 1 year ago
SELECT DATEADD(day, -ABS(CHECKSUM(NEWID())) % 365, GETDATE());
--random uppercase character
SELECT CAST(CAST(65 + ABS(CHECKSUM(NEWID()) % 26) AS binary(1)) AS char(1));Context
StackExchange Database Administrators Q#119068, answer score: 6
Revisions (0)
No revisions yet.