what is newid

What is NEWID() ?

NEWID Creates a unique value of type uniqueidentifier.

Its return type is uniqueidentifier.

[sql]SELECT NEWID() [/sql]

The output of the above query will be a unique identifier, some value like 8845D456-56FF-445Y-DF49-7GGTY54D3E3E

NewID can also be used to sort the rows in randomly as shown below

[sql]CREATE TABLE #TEMPNEWID(COL1 int)

INSERT INTO #TEMPNEWID(COL1)
SELECT 1
UNION
SELECT 2
UNION
SELECT 3
UNION
SELECT 4
UNION
SELECT 5
UNION
SELECT 6

SELECT COL1 FROM #tempnewid ORDER BY NEWID()[/sql]

The above query will give values in table #TEMPNEWID ordered randomly.

Leave a Reply

Your email address will not be published. Required fields are marked *