In this SQL Server tutorial, we will go over how to use the RAND which is one of the many useful built-in function in to create random numbers in SQL Server. Once you have a database to test with let’s start exploring how to work and change this function and use it to your advantage.
How to Use the RAND function
Using the function is quiet easy. Simply call the SELECT to the function and state to return a RandomNumber, like so:
When you submit this line of code you will get a random string of numbers. What is happening is that you are asking a computer to generate a random number sequence and the output should be an unique number. Notice though, that every time your call this function that the value will always be between 0 and 1. Not a fan of have that many decimals in your results? There is a way of getting rid of the decimal places by CASTing it to INT. In short, you must multiply that output to the integer value. Here is an example:
SELECT CAST(RAND() * 1000000 AS INT) AS [RandomNumber]
This will output a random number and move the decimals six spaces. This will turn the number .293847 (A given random number) into 293847.
Re-order the RAND() function output
You can also use the RAND() function to randomize the order of rows or values by using the ORDER BY RAND().
SELECT * FROM test_tbl ORDER BY RAND();
With test_tbl being the name of the our table, we are requesting to search all rows and randomize the order.
Wrapping It Up
Having a Randomizing Function can be a helpful tool and save you time calculating. Let functions handle the heavy tasks and monotony for you.
BEST RECOMMENDED SQL SERVER HOSTING
ASPHostPortal.com is Perfect, suitable SQL SERVER hosting plan. ASPHostPortal the leading provider of Windows hosting and affordable SQL SERVER Hosting. ASPHostPortal proudly working to help grow the backbone of the Internet, the millions of individuals, families, micro-businesses, small business, and fledgling online businesses. ASPHostPortal has ability to support the latest Microsoft and ASP.NET technology, such as: WebMatrix, WebDeploy, Visual Studio 2015, .NET 5/ASP.NET 4.5.2, ASP.NET MVC 6.0/5.2, Silverlight 6 and Visual Studio Lightswitch, ASPHostPortal guarantees the highest quality product, top security, and unshakeable reliability, carefully chose high-quality servers, networking, and infrastructure equipment to ensure the utmost reliability.
HostForLIFE.eu guarantees 99.9% uptime for their professional SQL SERVER 2014 hosting and actually implements the guarantee in practice. HostForLIFE.eu is the service are excellent and the features of the web hosting plan are even greater than many hosting. HostForLIFE.eu offer IT professionals more advanced features and the latest technology. HostForLIFe Best , Cheap SQL SERVER 2014 Hosting, Relibility, Stability and Performance of servers remain and TOP priority. Even basic service plans are equipped with standard service level agreements for 99.99% uptime. Advanced options raise the bar to 99.99%. HostForLIFE.eu revolutionized hosting with Plesk Control Panel, a Web-based interface that provides customers with 24×7 access to their server and site configuration tools.
DiscountService.com.au is The Best and Cheap SQL SERVER Hosting. DiscountService.com.au was established to cater to an under served market in the hosting industry web hosting for customers who want excellent service. DiscountService.com.au guarantees the highest quality product, top security, and unshakeable reliability, carefully chose high-quality servers, networking, and infrastructure equipment to ensure the utmost reliability. DiscountService.com.au has ability to support the latest Microsoft and ASP.NET technology, such as: WebMatrix, WebDeploy, Visual Studio 2015, .NET 5/ASP.NET 4.5.2, ASP.NET MVC 6.0/5.2, Silverlight 6 and Visual Studio Lightswitch. DiscountService.com.au is devoted to offering the best Windows hosting solution for you