Logo

dev-resources.site

for different kinds of informations.

SQL-Quick tip #4 - Random Int for each row

Published at
3/15/2022
Categories
tsql
sqlserver
sql
mssql
Author
coderallan
Categories
4 categories in total
tsql
open
sqlserver
open
sql
open
mssql
open
Author
10 person written this
coderallan
open
SQL-Quick tip #4 - Random Int for each row

Sql Server tips and tricks

This is part of a series of quick tips and tricks I have accumulated over the year, that I think can be useful for others.
If you have similar short tips and tricks please leave a comment.

Random int for each row

If you ever need a random number for each of the rows in your query result then you might think of the RAND() TSql function, but it turns out that this function will return the same value for every row in the result set. The trick to generate a random number for each row is to use the NEWID() and from that calculate the CHECKSUM which we then can turn into a positive number and the calculate the modulus with the maximum random number we want.

The code below show how to create a number between 0 and 24 and another example of how to create a number between 15 and 24.

The calculation of the random int for each row is not very efficient, so you should consider using other more efficient method for generating the random numbers if you need it in production code.



DECLARE @names TABLE ([Name] VARCHAR(50))

INSERT INTO @names ([Name]) 
VALUES ('Joe'), ('Bob'), ('Anne'), ('Jane')

-- Generate random int between 0 and 24
DECLARE @maxRandom INT = 25
SELECT [Name], CAST(ABS(CHECKSUM(NEWID())) % @maxRandom AS INT) [Random]
  FROM @names

-- Generate random int between 15 and 24
DECLARE @minRandom INT = 15
SELECT [Name], CAST(ABS(CHECKSUM(NEWID())) % (@maxRandom-@minRandom) AS INT) + @minRandom [Random]
  FROM @names


Enter fullscreen mode Exit fullscreen mode

Sql Server Management Studio screenshot

tsql Article's
30 articles in total
Favicon
T-SQL avanzato: tecniche da ricordare
Favicon
SQL Server Management Studio (SSMS)
Favicon
T-SQL , Stored Procedures UNIT Testing - Part 2
Favicon
How to check if a temporary table exists and delete it if it does before creating a temporary table?
Favicon
How to search for text in a SQL Server stored procedure, function, or view?
Favicon
SQL Server 2022 - GENERATE_SERIES
Favicon
SQL Server 2022: Logical Functions - GREATEST & LEAST
Favicon
SQL-Quick tip #15 - Random dates
Favicon
SQL-Quick tip #13 - Index usage
Favicon
SQL-Quick tip #14 - Server information
Favicon
SQL-Quick tip #8 - Finding foreign key constraints
Favicon
SQL-Quick tip #12 - Available disk space
Favicon
SQL-Quick tip #10 - Select table definition
Favicon
SQL-Quick tip #11 - Most intensive queries
Favicon
SQL-Quick tip #9 - Number of rows in all tables
Favicon
How to find an account balance in SQL?
Favicon
SQL-Quick tip #7 - Find stored procedures
Favicon
SQL-Quick tip #6 - Find table or column
Favicon
SQL-Quick tip #5 - Create a sequence of date and time
Favicon
SQL-Quick tip #2 - Randomize rows
Favicon
SQL-Quick tip #4 - Random Int for each row
Favicon
SQL-Quick tip #1 - Range of Int
Favicon
SQL Server Primary Keys
Favicon
Calculating length of ntext data type with T-SQL
Favicon
What are the gotchas when converting a T-SQL statement into a JavaScript RegExp?
Favicon
How to prevent duplicate count value for inner join
Favicon
SQL Server: The Identifier Is Too Long; Max Length Is 128
Favicon
Geohash Open-Source library in TSQL for SQL Server
Favicon
How to monitor backup and restore progress in SQL Server
Favicon
SQL Query Inner Join for SUM Amount

Featured ones: