Skip to content

Instantly share code, notes, and snippets.

@dmlogv
Last active June 17, 2019 10:25
Show Gist options
  • Save dmlogv/40a17e89053a12ebb178e0185b266eab to your computer and use it in GitHub Desktop.
Save dmlogv/40a17e89053a12ebb178e0185b266eab to your computer and use it in GitHub Desktop.
Random integer number in Transact-SQL (Microsoft SQL Server)
/*
Slow method
Pre-use my integer's table: https://gist.github.com/dm-logv/38eb66f5a2a461cb8f42e14714682daa
*/
SELECT TOP 1 i
FROM #integers
ORDER BY NEWID();
/*
Mid-fast method
Range [0; @max)
*/
DECLARE @max INT = 1000000;
SELECT ABS(CHECKSUM(NEWID())) % @max;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment