Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting on GUID SQL Server 2008

What is the best way to sort records on Guid. Below never gives right results.

    With UIDs As (--                        
            Select ID =  1, UID = NEWID() 
    Union   Select ID =  2, UID = NEWID()
    Union   Select ID =  3, UID = NEWID()
    Union   Select ID =  4, UID = NEWID()
    Union   Select ID =  5, UID = NEWID()
    Union   Select ID =  6, UID = NEWID()
    Union   Select ID =  7, UID = NEWID()
    Union   Select ID =  8, UID = NEWID()
    Union   Select ID =  9, UID = NEWID()
    Union   Select ID = 10, UID = NEWID()
    Union   Select ID = 11, UID = NEWID()
    Union   Select ID = 12, UID = NEWID()
    Union   Select ID = 13, UID = NEWID()
    Union   Select ID = 14, UID = NEWID()
    Union   Select ID = 15, UID = NEWID()
    Union   Select ID = 16, UID = NEWID()
)
Select * From UIDs Order BY  UID, ID

Instead using Guid, how can i generate Sequential Integers in my select.

Thanks

like image 206
Sreedhar Avatar asked Sep 06 '10 23:09

Sreedhar


Video Answer


1 Answers

SELECT * 
FROM myTable 
ORDER BY CAST(myGuid AS VARCHAR(36))
like image 154
Mark Avatar answered Oct 19 '22 15:10

Mark