SQL Server 2008 NEWID Function

In this article i will explain how to use the NEWID() function as random generator in SQL Server.
  • 2895

Introduction

NEWID() function is used in generating random rows from a resultset in SQL Server database. We can also call it as SQL Random Generator. This function is used to select records randomly from a set of rows or from a table. Basic example for using this function is explained below.

Example

Suppose we have table "Employee" with columns (ID, F_Name, L_Name, Salary, Region, City ) shown in below image.

CreateTable-in-SQL.jpg
 

Now to select records on random basis. Use TOP N keyword in select statement where NEWID() is in the ORDER BY statement.

RandomGenerator-in-sql.jpg

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.