Order by GUID/UUID in SQL Server
We have some tables in MS SQL land where the ID fields are GUIDs (also known as UUIDs), and often you want to sort by ID when you run a query:
select * from kiosk order by KioskId
…but it doesn’t come out in the kind of “alphabetical” order you would expect. This happens to be because of the precedence of bytes within GUID, which is pretty wild.
If you want a sensible alphabetical output, you can cast the field in your
order by clause to an
select * from kiosk order by cast(KioskId as nvarchar(37))